0

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?

1
  • Use ajax to post data to php. Commented Sep 23, 2015 at 11:05

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.