Reputation: 1689
I get info in javascript dialog like this:
<!DOCTYPE html>
<html>
<body>
<script>
var myInfo = prompt("Please enter info", "");
if (myInfo != null) {
//Here is my info
}
</script>
</body>
</html>
How can I send this "myInfo" to PHP file (in the same server of javascript file) via GET, POST or by other method?
Upvotes: 0
Views: 59
Reputation: 1056
Instead of using native ajax you can use jquery and ajax to send data like below
<!DOCTYPE html>
<html>
<body>
<script src="jquery-1.11.3.min.js"></script>
<script>
var myInfo = prompt("Please enter info", "");
if (myInfo != null) {
$.ajax({
url: "php_page.php",
data: {
info: myInfo
},
success: function( data ) {
alert( "data sent" );
}
});
}
</script>
</body>
</html>
You will have to use jquery.js inorder to use this ajax functionality. On your php page you can directly refer the variable using $_REQUEST method as below
<?php
$data= $_REQUEST['info'];
?>
for more info on how jquery works you can refer to jquery.com
Upvotes: 0
Reputation: 252
This is fairly easy with ajax,
PHP Code:
<?php
$data=$_GET['data'];
//Do something with it
echo 'Response';
die();
?>
HTML Code:
<!DOCTYPE html>
<html>
<body>
<script>
var myInfo = prompt("Please enter info", "");
if (myInfo != null) {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response=xmlhttp.responseText;
//Do someting with it
}
}
xmlhttp.open("GET","/file.php?data="+myInfo,true);
xmlhttp.send();
}
</script>
</body>
</html>
Upvotes: 1