Echo PHP Variable from JavaScript

Echo PHP variable from JavaScript?

In your second example, you are missing quotes around the string (so H is interpreted as a variable - which you didn't set).

Test this:

alert (<?php echo "'H'";?>);

OR

alert ('<?php echo "H";?>');

How to assign Php variable value to Javascript variable?

Essentially:

<?php
//somewhere set a value
$var = "a value";
?>

<script>
// then echo it into the js/html stream
// and assign to a js variable
spge = '<?php echo $var ;?>';

// then
alert(spge);

</script>

How to access PHP variable in JavaScript code?

You can access a PHP variable inside javascript by echoing it within quotes if the value is a string and just need to echo if it is an integer, like;

var i=<?php echo $i; ?>;  

How can I store JavaScript variable output into a PHP variable?

You have to remember that if JS and PHP live in the same document, the PHP will be executed first (at the server) and the JS will be executed second (at the browser)--and the two will NEVER interact (excepting where you output JS with PHP, which is not really an interaction between the two engines).

With that in mind, the closest you could come is to use a PHP variable in your JS:

<?php
$a = 'foo'; // $a now holds PHP string foo
?>
<script>
var a = '<?php echo $a; ?>'; //outputting string foo in context of JS
//must wrap in quotes so that it is still string foo when JS does execute
//when this DOES execute in the browser, PHP will have already completed all processing and exited
</script>
<?php
//do something else with $a
//JS still hasn't executed at this point
?>

As I stated, in this scenario the PHP (ALL of it) executes FIRST at the server, causing:

  1. a PHP variable $a to be created as string 'foo'
  2. the value of $a to be outputted in context of some JavaScript (which is not currently executing)
  3. more done with PHP's $a
  4. all output, including the JS with the var assignment, is sent to the browser.

As written, this results in the following being sent to the browser for execution (I removed the JS comments for clarity):

<script>
var a = 'foo';
</script>

Then, and only then, will the JS start executing with its own variable a set to "foo" (at which point PHP is out of the picture).

In other words, if the two live in the same document and no extra interaction with the server is performed, JS can NOT cause any effect in PHP. Furthermore, PHP is limited in its effect on JS to the simple ability to output some JS or something in context of JS.

Printing PHP variables into JavaScript variables

Use this no need to give "" => change to '.$test1.'..

<?php
$test1 = '1';

print '
<script type="text/javascript">
var carnr;
carnr = "'.$test1.'"
console.log(carnr);
</script>';
?>

PHP Variable inside PHP - Echo - Javascript

You'll want to use string concatenation (using the . character) to insert a variable into your string. Like this:

echo '
[...]
<script type="text/javascript">
var dID = ' . $dID . ';
function wait(ms){
[...]
';

A . will concatenate two strings together. For example:

echo 'hello ' . ' world'

You can also insert a variable directly into a string, if you use double quotes. Single quotes do not allow you to do this:

$text = "world";
echo "hello $text";

In general, you should wrap your variables in curly brackets ({ and })

$text = "world";
echo "hello {$text}";

how to use PHP variable in Javascript

Maybe you could do something like:

<script>
var a = <?php echo $a; ?>; //for numbers
</script>

That's one basic way of accomplishing what you want.

EDIT: As JiteshNK also pointed out, you could also do:

<script>
var a = <?php echo json_encode($a); ?>; //safest solution
</script>

Cant echo PHP variable inside script tags?

The real problem is that this is what your rendered result looks like:

var error = You must choose a project.
Click ‘back’ and try again.

Does that looks like valid JavaScript to you? I think not.

var error = <?=json_encode($error);?>;

That should result in:

var error = "You must choose a project.\r\n                 Click ‘back’ and try again.";

Much better.

Pass PHP variable within echo script content

echo "window.location = 'http://example.com/something-else/$php_variable'";

How do I pass JavaScript variables to PHP?

You cannot pass variable values from the current page JavaScript code to the current page PHP code... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.

You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.

<DOCTYPE html>
<html>
<head>
<title>My Test Form</title>
</head>

<body>
<form method="POST">
<p>Please, choose the salary id to proceed result:</p>
<p>
<label for="salarieids">SalarieID:</label>
<?php
$query = "SELECT * FROM salarie";
$result = mysql_query($query);
if ($result) :
?>
<select id="salarieids" name="salarieid">
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one)
}
?>
</select>
<?php endif ?>
</p>
<p>
<input type="submit" value="Sumbit my choice"/>
</p>
</form>

<?php if isset($_POST['salaried']) : ?>
<?php
$query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid'];
$result = mysql_query($query);
if ($result) :
?>
<table>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others
echo '</tr>';
}
?>
</table>
<?php endif?>
<?php endif ?>
</body>
</html>


Related Topics



Leave a reply



Submit