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.
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.