Jeff
Jeff

Reputation: 21

Jquery post to php file works but can only return some data when echoed in PHP

//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.

Upvotes: 0

Views: 277

Answers (2)

Chris Clower
Chris Clower

Reputation: 5104

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/

Upvotes: 0

bkconrad
bkconrad

Reputation: 2650

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?

Upvotes: 1

Related Questions