javascriptphpajaxjquery-2.0

It is possible to get the variables sent by Ajax in php file


I have my form defined as follows in the PHP file (Using jquery-2.1.4.min.js) :

<form id = "testForm" action="" method="post" >
    First name: <input type="text" name="FirstName" id="FirstName" value="Mickey"><br>
    Last name: <input type="text" name="LastName" id="LastName" value="Mouse"><br>
    <button type = "button" onclick="submit()">Submit Top Test </button>
</form>

The following functions is called when the button is clicked.

function submit(){  
        var firstName = $('#FirstName').val();
        alert("Inside SubmitQuery  "+firstName);// Work fine

        var request = $.ajax({
                url: 'php/<path to php file>/processPost.php',
                type:'POST',
                data:{title:firstName},
                success: function(msg) {  

                     alert(msg);// I can see the var_dumps here

                 }

                });

    }   

In processPost.php file, I have defined the following two var_dumps :

1) var_dump(isset($_POST['title']));

2) var_dump ($_POST['title']);

I can see the values getting printed in the alert(msg) window. Same is true in the Response tab of the Network tab of developers tools window. But I want to have these variables available in the processPost.php file so that I could use it to make a curl request to webservice call. Is it possible to get these variables inside processPost.php file?


Solution

  • I mean, you basically answered your own question in your question. The fact that you have this file:

    processPost.php:

    <?php
        var_dump( $_POST['title'] );
    ?>
    

    and your success: function(msg){ alert(msg); } is properly alerting "Coder" (or whatever name you submit) Literally means that the variable $_POST['title'] is available in your processPost.php file. If alert(msg) gave you nothing, or an error, then you'd know something is wrong.

    success: function(msg) means that, if successful, take the output value from your url (in this case, processPost.php), and make it available to your javascript as a variable named msg.