Pass a value from javascript to php [duplicate] - javascript

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 6 years ago.
I want to send the value of address to a new PHP page
<script>
var address = place.formatted_address;
document.getElementById('af').innerHTML = address;
</script>

PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:
<script type="text/javascript">
var foo = '<?php echo $foo ?>';
</script>
To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):
var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});
On your server, you would need to receive the variable sent in the post:
$variable = $_POST['variable'];

Related

PHP variable can be assign to javascript variable but javascript variable can't assign to php variable [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 3 years ago.
If we have a variable in php which holds a value then we can assign easily this value to js variable but the vice versa is possible?
For Assign php variable to js variable we use
<?php $r = 1; ?>
<script>
var s = "<?php echo $r; ?>";
</script>
But for assign js variable to php variable its not works
<script>
var s = 1;
</script>
<?php $r = echo "<script> s </script>"; ?>'
Not working
Generally, PHP is server side while Javascript is client side. You can do it by creating a browser cookie using Javascript and then use PHP to read that cookie.
Anyway, why do you want to do that?

variable in php to variable in javascript [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
I want to take my variable in php, and put in a variable in javascript
there is my code in php :
$SQL=$pdo->query("SELECT SUM(nbdeces) as totalnbdeces FROM nombre_deces");
$data = $SQL->fetch();
$sommeanswered = $data['totalnbdeces'];
I want to stock the variable "$sommeanswered" in a variable javascript
You can do that like
<script>
var val = "<?php echo $sommeanswered; ?>";
</script>

Get Yii user state variables in javascript [duplicate]

This question already has answers here:
In Yii, pass PHP variables to JavaScript
(6 answers)
Closed 8 years ago.
I am using Yii::app()->user->setState to set some user's session variables.
Is there a way to get the values of them from within JavaScript?
Something like this perhaps? You have to echo it to the JavaScript.
<script>
var myVariable = "<?= Yii::app()->user->getState('myVariable', '') ?>";
alert(myVariable);
</script>

Global php variable doesn't affect output of function [duplicate]

This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Closed 8 years ago.
I am trying to implement this example on my website: http://jsfiddle.net/eDeQr/1/
The problem is that I need the $cp1 = 4;variable to be global and not inside that function. As soon as I do that the function doesn't give any output in the second field.
This is what I want it to look like:
<script type="text/javascript">
function calc() {
var textValue1 = document.getElementById('input1').value;
var textValue2 = $cp1;
document.getElementById('output').value = textValue1 * textValue2;
};
</script>
The $cp1 variable should be global.
You're mixing JavaScript and PHP. You actually have to echo out the PHP variable inside of your JavaScript. Otherwise the JavaScript is looking for a JavaScript variable named $cpl which is undefined.
var textValue2 = <?php echo $cp1; ?>

Passing Javascript String into php String [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Passing javascript variables to php?
(5 answers)
Closed 8 years ago.
let's say i have:
<script>
var jsString="hello";
</script>
and i want it to pass into php string:
$phpString = jsString;
how do i do that correctly?
please tell me the right way. thanks in advance.
You need a Ajax call to pass the JS value into php variable
JS Code will be (your js file)
var jsString="hello";
$.ajax({
url: "ajax.php",
type: "post",
data: jsString
});
And in ajax.php (your php file) code will be
$phpString = $_POST['data']; // assign hello to phpString
You will need to use an HTTP POST to send the data to PHP. Check out this tutorial: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php to see how to send a post without JQuery. Also see the XMLHTTPRequest docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest. As other answers have noted, JQuery is the makes this much easier with $.post: http://api.jquery.com/jquery.post/.
To get the string in PHP use the $_POST variable.

Categories