JavaScript innerHtml not changing to php session - javascript

I am trying to change html button's text with php and javascript but it doesn't give errors and i don't know what is wrong.
My code that i am trying to get working.
At start of the file:
<?php
session_start();
$_SESSION["LOGGED"] = "Log in";
?>
Then comes the button code
<button class='button' id='login'>button</button>
<script type="text/javascript">
<?php
echo "var msg = '" .$_SESSION["LOGGED"] . "'";
?>
document.getElementById('login').innerHTML = msg;
</script>
What is wrong? The text just doesn't change.
Edit: The 2 sciprts php part crashes the script? It says unexpected token <. Removed the php part and putted to .innerHTML = "test"; and the button changed to test. So it should be somewhere at the php part?

If you're using the <button> tag,
document.getElementById('login').innerHTML = msg;
should work.
But if you're using <input type="button">, then you will have to use .value instead of .innerHTML
document.getElementById('login').value = msg;
EDIT: Try this...
<script>
var msg = <?php echo $_SESSION["LOGGED"]; ?>; //Don't forget the extra semicolon!
document.getElementById('login').innerHTML = msg;
</script>

Is your file a .php file and does the php work correctly?
try testing your php by putting
<?php echo "Hello world!"; ?>
at the top of your page :)

Related

Auto-fill form on WordPress

I am using the Scripts n Styles plugin with this code to auto-fill a logged in user's email into form on WordPress:
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("Element-Name")[0].value = "<?php $current_user->user_email ?>";
}
</script>
However, when I refresh the page I get the following text (the quoted value in the code above) instead of the actual value.
<?php $current_user->user_email ?>
Any idea what I am missing here? Is this an issue with the plugin?
The first thing I see is that you don't output anything.
Try
<?php echo $current_user->user_email; ?>
But in your example it should have been just an empty value. There is something fishy with the PHP parser not detecting the PHP part.

PHP variable is not included when loading with javascript .how to include php inside javascript?

My js code re-loads the comments.php in every 3 seconds .
I have included comments.php in index.php and it does get all the variables from index.php but when js reloads comments.php it does not get the variable and shows error .
First time it works cause it's included with php . After the re-load it's not included anymore .
How can i include comments.php in index.php inside js.
index.php
<?php
if (isset($_GET['slug'])){
$page_id = $_GET['slug'];
}
?>
<div id="comments">
<script type="text/javascript" >
$(document).ready(function(){
setInterval(function(){
$('#comments').load('comments.php')
},3000);
});
// when loading comments.php with js $page_id is not gotten and showing error
</script>
<?php include("comments.php") ; ?>
// first time shows this and comments.php does get the varible $page_id
</div>
comments.php
<?php
$page_id = $_GET['slug'];
$get_com = " SELECT * FROM `comments` where post_id='$page_id' ORDER BY `comment_id` DESC ";
$run_com = mysql_query($get_com);
?>
When you include("comments.php") it inherits the variables from index.php. When you reload with load('comments.php') you are doing it via HTTP and there is no GET variable slug because you are not passing it:
$('#comments').load('comments.php?slug=<?=urlencode($page_id)?>')
The simplest way to do this is to simply echo the PHP variable to JavaScript:
var page_id = '<? echo $page_id ?>';
var get_com = '<? echo $get_com ?>';
var run_com = '<? echo $run_com ?>';
Assuming those variables are populated by the time the PHP is finished, JavaScript will be able to make use of them.

How to pass the value from url of php to .js

Hi I am new to javascript and I am trying to pass a value from Get in url of php to javascript.
This is my code.
this is the url
someproject/index.php?roomId=2
index.php
room = $_GET['roomId'];
<input hidden id="room" value=<?=$room;?>>
script.js
I want to get the value from php and I want something like:
$('#room').getValueFromPHP();
but I do not know how to do it. Please help me.
This should do it:
PHP
<input type="hidden" id="room" value="<?=$room;?>">
JavaScript
$(function() {// make sure the document is ready.
var url = $("#room").val();
});
In order to pass a value from PHP to JavaScript, you can use echo as follows:
<?php $room = $_GET['roomId']; ?>
<script> var room = <?php echo "'$room'"; ?>; </script>

How do I embed Javascript in PHP code?

I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "var oldnotes = document.getElementById('oldnotes');";
echo "oldnotes.disabled = false;";
echo "var record = document.getElementById('record');";
echo "record.disabled = false;";
echo "</script>";
}
I have also tried;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "$('#oldnotes').removeAttr('disabled')";
echo "$('#record').removeAttr('disabled')";
echo "</script>";
}
But no luck :(
I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.
This is a better approach for these kind of problems :
if (isset($_POST['edit'])){
?>
<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>
<?php
}
<?
<script language='javascript'>
(javascript code here)
</script>
?>
Try use onclick on your Button
<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">
I hope it helps.
The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.
Here's what I suggest:
<?php
if (isset($_POST['edit'])) {
?>
<script type="text/javascript">
alert("Check entry"); // Use this for checking if your script is reaching here or not
$('#oldnotes, #record').removeAttr('disabled');
</script>
<?php
}
?>
You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.
Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.
If not, something wrong with your edit button and the form POST
Use single quotes for script type
echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
use this jquery code:
<script>
$('#oldnotes,#record').attr('disabled','')
</script>
If you want to use JS with PHP you can read here:
how to write javascript code inside php
However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.
Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.
<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >
Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.
Hope it helps!
you can always make a cookie
use document.cookie()
set the onclick=“” to changing the cookie
and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})

Posting a javascript variable into a text area using PHP

I am using PHP to post into a textarea:
<textarea name="">
<?php echo $name?>
</textarea>
I have tried to do this.
Jquery:
var yo = "Hello"
$.post(window.location, {variable: yo});
PHP:
<?php
$name = $_POST['variable'];
?>
Why does this not write "Hello" in the textarea / Why is the php not receiving the .post?
can you try <?php $_SERVER['PHP_SELF']); ?> as URL at your jQuery post function instead of window.location as follow
$.post(<?php $_SERVER['PHP_SELF']); ?>, {variable: yo});
Assuming you want to change a value inside a textarea with a value you calculate or set in javascript, you can assign it this way using JQuery:
<script>
var yo = "hello";
$("#test").html(yo); //where test is the ID from the textarea
</script>
<textarea id="test">
</textarea>
FIDDLE: http://jsfiddle.net/YcgsR/
You can modify your logic a bit here by making your PHP script take in your JS variable and do some logic/manipulation/query/whatever (that you can't do via JS) and return a value which you want to appear in the textarea. You can then leverage this return value via an AJAX request like so:
var yo = "Hello";
$.post(window.location, {variable: yo}, function(value) {
$('textarea').val(value);
});
and in your PHP script, something like:
<?php
$name = $_POST['variable'];
/*
* do manipulation here that you can't do purely via JS
* otherwise why would you be posting to a PHP script?
*/
echo $some_return_value;
?>

Categories