0

//Jquery code to send the data with AJAX

        $.ajax({
            type: "POST",
            url: "test.php",
            data:
            "fname="+ fname +
            "& lname="+ lname +
            "& address="+ address +
            "& city="+ city +
            "& state="+ state +
            "& zip="+ zip +
            "& phone="+ phone +
            "& useremail="+ useremail +
            //the following values are not being receieved by the php correctly
            "& subtotal="+ subTotal +
            "& quantity="+ quantity,
            success: function(){
            $('#oderBtn').hide(function({$('#orderTest').fadeOut();});
            }
        });

//PHP CODE TO RECEIVE THE AJAX DATA

$fname = htmlspecialchars(trim($_POST['fname']));
$lname = htmlspecialchars(trim($_POST['lname']));
$city = htmlspecialchars(trim($_POST['city']));
$state = htmlspecialchars(trim($_POST['state']));
$zip = htmlspecialchars(trim($_POST['zip']));
$address = htmlspecialchars(trim($_POST['address']));
$email = htmlspecialchars(trim($_POST['useremail']));

//these do not post correctly, i do not know why
$subTotal = htmlspecialchars(trim($_POST['subtotal']));
$quantity = htmlspecialchars(trim($_POST['quantity']));

So the problem is that fname, lname, city, state, zip, address, and email are all working but subtotal, and quantity are not working, firebug has them all POSTing in the same way, it seems like the PHP is just not recieving the data properly.

Adding echo file_get_contents("php://input"); to the php does get everything sent echoed back, including subtotal and quantity but just doing $_POST['subtotal'] will not get the value.

Thanks for any assistance in this matter.

1
  • That seems like a strange way to format the data. Have you tried formatting the data like this: data: { fname: fname, lname: lname, address: address, ... } ? Commented Aug 6, 2012 at 22:39

2 Answers 2

1

Well, first, you don't need to build a string like that. You can simply pass an object literal:

    $.ajax({
        type: "POST",
        url: "test.php",
        data: {
            fname: fname,
            lname: lname
            ...
        },
        success: function(){
        $('#oderBtn').hide(function({$('#orderTest').fadeOut();});
        }
    });

And jQuery will serialize it as necessary. I think it's impressive that it's honoring your POST request at all since you're passing it a query string.

Also, it looks suspicious that it stops working after the email. can you paste the result of print_r($_POST); in PHP?

Sign up to request clarification or add additional context in comments.

1 Comment

Array ( [fname] => [amp;_lname] => [amp;_address] => [amp;_city] => [amp;_state] => AL [amp;_zip] => [amp;_phone] => [amp;_useremail] => [amp;_subtotal] => 5 [amp;_quantity] => 1 ) Here is the response from print_r($_POST); keep in mind i did not fill in any of the data in the form other than the values in question.
0

Have you tried using $_GET[] rather than $_POST[]? That would be the first thing I'd try, since $_GET is designed for getting data from a URL, whereas $_POST is designed more for getting data passed in from a form submit.

$fname    = htmlspecialchars(trim($_GET['fname']));
$lname    = htmlspecialchars(trim($_GET['lname']));
$city     = htmlspecialchars(trim($_GET['city']));
$state    = htmlspecialchars(trim($_GET['state']));
$zip      = htmlspecialchars(trim($_GET['zip']));
$address  = htmlspecialchars(trim($_GET['address']));
$email    = htmlspecialchars(trim($_GET['useremail']));
$subTotal = htmlspecialchars(trim($_GET['subtotal']));
$quantity = htmlspecialchars(trim($_GET['quantity']));

An example of this output as a JSON array:

$data = array(
    'fname'    => htmlspecialchars(trim($_GET['fname']));
    'lname'    => htmlspecialchars(trim($_GET['lname']));
    'city'     => htmlspecialchars(trim($_GET['city']));
    'state'    => htmlspecialchars(trim($_GET['state']));
    'zip'      => htmlspecialchars(trim($_GET['zip']));
    'address'  => htmlspecialchars(trim($_GET['address']));
    'email'    => htmlspecialchars(trim($_GET['useremail']));
    'subTotal' => htmlspecialchars(trim($_GET['subtotal']));
    'quantity' => htmlspecialchars(trim($_GET['quantity']));
);

echo json_encode($data);

EDIT:

You may also need to use encodeURI() in your jQuery to ensure that no ampersands or other characters mess up the URL string.

$.ajax({
    type: "GET",
    url: "test.php",
    data: encodeURI(
        "?fname="     + fname +
        "&lname="     + lname +
        "&address="   + address +
        "&city="      + city +
        "&state="     + state +
        "&zip="       + zip +
        "&phone="     + phone +
        "&useremail=" + useremail +
        "&subtotal="  + subTotal +
        "&quantity="  + quantity
    ),
    success: function(response){
        alert(response); // Will show the JSON array
        $('#oderBtn').hide(function({$('#orderTest').fadeOut();});
    }
});

Fiddle: http://jsfiddle.net/xNSLX/

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.