Search code examples
phpstringserververifyreceipt

How to verify receipt of my Ajax string query by the server?


I am using Javascript to send form data to a php via Ajax for validation and mailing. The query string which I named formString looks like this 'name= John Smith'. I have one input field only for testing purpose. The Ajax communication between my client page and the server is fine and I checked it successfully with the scripts shown below.

The problem is that I am not able to capture the formString query at the server-side. I am providing below the method I am used to capture the data unsuccessfully. The echo json_encode($name) is returning nothing to the html server.

I tried the query with several input fields values serialized and did not work. I tried to submit the query string a simple string including only the first name 'John', but it did not work either.

processForm()
var name = document.getElementById("fullName").value;
var formString = name;

var name = document.getElementById("fullName").value;
var formString = name;

var xhr = new XMLHttpRequest();
    xhr.open('POST', formfile.php, true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    xhr.onreadystatechange = function () {
      if(xhr.readyState == 4 && xhr.status == 200) {
        var result = xhr.responseText;

      xhr.send(formString);

button.addEventListener("click", function(event) {
    event.preventDefault();
    processForm();

PHP snippet:

header('Content-Type: application/json');

function is_ajax_request(){
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}
if(is_ajax_request()) {

$Ajax_results = array (
'Testing Text' => 'Hello World',
    'Your Ajax submittal succeeded.
);  
 echo json_encode($Ajax_results);

 } else {

$None_Ajax_results = array (
'errors' => 'None Ajax - short'
'Your Ajax submittal failed. Errors.'
);
echo "Form is Non Ajax Submitted";
echo json_encode($None_Ajax_error);
exit;
}

Define and set variables:

global $name;

$errors = null;

if (isset($_POST['name'])) { $name = $_POST['name']; }
else { $name = ''; }

echo '$name';
echo json_encode($name);

Solution

  • If I am reading your question correctly and assuming you have proper heartbeat between Ajax and the server as you state you do, taking a quick look at your code as provided you are not properly formatting your "formString". In order for your formString to properly show up in the $_POST['name'] it should be:

    var formString = "name="+name
    

    This is because the the post string being sent ("formString" in your case) should have the format:

    field1=val1&field2=val2& ... fieldN=valN
    

    where the name of each field is stated, followed by '=' and the value of the field. Multiple fields are separated by the'&' character. Which in PHP which will translate to

    $_POST = {field1=>val1, field2=>val2, ... fieldN=>valnN}
    

    on the server side. This is of course not literal code above but an example of the standard API. Take a closer look at how to format Post strings for HTML GET/POST