Skip to content Skip to sidebar Skip to footer

Passing Javascript String Into Php String

let's say i have: and i want it to pass into php string: $phpString = jsString; how do i do that correctly? please tell m

Solution 1:

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 

Solution 2:

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.

Post a Comment for "Passing Javascript String Into Php String"