1

I have an ajax script of which gatheres values from inputs and then inserts to a mysql database using MYSQLI.

When i click the button the unique id is returned successfully but none of the post values are being inserted into the database. Also when testing the post values individually they too do not show as reaching the posted php script.

After running this the unique id is in the database but no other information is stored.

Q. How come the variables not reaching the PHP, thus preventing me from a successful insertion to the database?

The Ajax

$('#postData').click(function() {
        smallVan_getForm_values();

    $.ajax({
     type: "POST",
     dataType: "json",
     url: "//xxxxx",
     data: JSON.stringify({
            book_email:book_email,
            book_clientTitle : book_clientTitle,
            book_firstname : book_firstname,
            book_lastname :book_lastname,
            book_dateofbirth : book_dateofbirth,
            book_primarycontactnumber : book_primarycontactnumber,
            book_secondarycontactnumber : book_secondarycontactnumber,
            departing_first_lineaddress : departing_first_lineaddress,
            departing_second_lineaddress : departing_second_lineaddress,
            departing_postcode : departing_postcode,
            destination_first_lineaddress : destination_first_lineaddress,
            destination_second_lineaddress : destination_second_lineaddress,
            destination_postcode : destination_postcode,
            amount : amount,
            book_consignmentdate : book_consignmentdate,
            book_timeofbooking : book_timeofbooking,
            book_additionalnotes : book_additionalnotes
    }),
     contentType: "application/json; charset=utf-8",

        success: function (result) {
        //  alert(result);

            if (result.status == "success"){
                    uniqueID = result.uniqueID;
                    alert(uniqueID);

            }

       }
    });

});

});
//AJAX END

The PHP

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{ //if request made from localserver then proceed here
    insertData();
}


function insertData() {

$uniqueID = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ", 10)), 0, 10);


    $conn=mysqli_connect("xxxx","xxx","xxxxx^","xxxxx");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    // Perform queries
    mysqli_query($conn,"INSERT INTO bookings_table ( booking_uniqueID , book_email , book_clientTitle , book_firstname , book_lastname , book_dateofbirth , book_primarycontactnumber , book_secondarycontactnumber , departing_first_lineaddress , departing_second_lineaddress , departing_postcode , destination_first_lineaddress , destination_second_lineaddress , destination_postcode , amount , book_consignmentdate , book_timeofbooking , book_additionalnotes )
   VALUES ( '$uniqueID' , '$_POST[book_email]' , '$_POST[book_clientTitle]' , '$_POST[book_firstname]' , '$_POST[book_lastname]' , '$_POST[book_dateofbirth]' , '$_POST[book_primarycontactnumber]' , '$_POST[book_secondarycontactnumber]' , '$_POST[departing_first_lineaddress]' , '$_POST[departing_second_lineaddress]' , '$_POST[departing_postcode]' , '$_POST[destination_first_lineaddress]' , '$_POST[destination_second_lineaddress]' , '$_POST[destination_postcode]' , '$_POST[amount]' , '$_POST[book_consignmentdate]' , '$_POST[book_timeofbooking]' , '$_POST[book_additionalnotes]' )");

    mysqli_close($conn);
    echo json_encode(array('status' => 'success', 'uniqueID' => $uniqueID));

}
3
  • If you're getting the uniqueID back, then clearly mysqli_query is being called. What value is mysqli_query returning? Commented Feb 25, 2015 at 2:07
  • i am getting the uniqueID back yes. But the posted data did not get inserted as it doesnt appear to be posting to the sql. The sql is inserting only the uniqueID becuase that appears all to be there. Tested this with posting the vars at the php page, they are not reaching the php page via that ajax. Commented Feb 25, 2015 at 2:41
  • Sorry, I misunderstood your problem. Glad to see that someone else understood it though! Commented Feb 25, 2015 at 5:59

1 Answer 1

2

just remove your JSON.stringify().

data: {
            book_email:book_email,
            book_clientTitle : book_clientTitle,
            book_firstname : book_firstname,
            book_lastname :book_lastname,
            book_dateofbirth : book_dateofbirth,
            book_primarycontactnumber : book_primarycontactnumber,
            book_secondarycontactnumber : book_secondarycontactnumber,
            departing_first_lineaddress : departing_first_lineaddress,
            departing_second_lineaddress : departing_second_lineaddress,
            departing_postcode : departing_postcode,
            destination_first_lineaddress : destination_first_lineaddress,
            destination_second_lineaddress : destination_second_lineaddress,
            destination_postcode : destination_postcode,
            amount : amount,
            book_consignmentdate : book_consignmentdate,
            book_timeofbooking : book_timeofbooking,
            book_additionalnotes : book_additionalnotes
    },
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this and it still does not insert anything.
when i test this with a simple POST ajax they are all returning correctly, its when i begin to use the json that they do not.
i just trying your code with some post parameters, and i remove JSON.stringify() and contentType: "application/json; charset=utf-8", its worked. your data was formated in JSON, your parameter dataType was set to JSON before. and contentType default encoding is charset=utf-8, so i remove it.
I missed to take out contentType: "application/json; charset=utf-8" it now works, ok thank you so much!

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.