0

I've a simple select which on change posts data through ajax. On the same page I want to get the data from POST array but the post array is empty. This is the code.

Javascript

$('.product-selectbox').change(function() {
    var select_id = $(this).attr("id");
    var presentkort_type = $(this).val();
    presentkort_type = presentkort_type;
    alert(presentkort_type);
    //alert("id is "+id);
    var request =  $.ajax({
        url : "cashregister",
        type : "POST",
        data : {
            prod_id : select_id, presenkort : presentkort_type
        },
        dataType : "html"
    });
    request.done(function(msg) {
        $('#data').html(select_id);
        console.log(select_id);
        console.log(presentkort_type);
        location.reload(true);
    });
    request.fail(function(msg) {});
});

PHP

if (isset($_POST['prod_id'])) {
    $product_id = (int)$_POST['prod_id'];
    print "product id is ".$product_id;
} else {
    print "no data";
    $product_id = (int)$_POST['prod_id'];
}
3
  • I would suggest you open the console in your browser and monitor the status of the XHR request. Ensure that the relative URL you're specifying exists on your server yoururl.com/cashregister . On chrome press Ctrl+Shift+I to open the console, then go to Network->XHR Commented Jun 12, 2013 at 13:18
  • The url exists, I can see the data if I log it to console. but the POST array on the page is empty. Commented Jun 12, 2013 at 13:48
  • Do a series of var_dump() on your PHP side for $_POST, $_GET and $_REQUEST to see what is being sent. Commented Jun 13, 2013 at 13:47

4 Answers 4

2

I encountered a similar problem when I was posting an object to my server as well. It turned out that my content was actually be passed in the request body.

To access the content I had to use the following:

$data = file_get_contents("php://input");

This allowed me pull the object out of the request body and placed it in a usable variable that I could manipulate.

Here is the reference I used: PHP Wrappers

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

1 Comment

I scratched my head with this too - sending ajax requests from an angularjs/cordova android app this is the only way to get at the vars.
0

remove datatype as html ,it should be like this

 var request =  $.ajax({
        url : "cashregister",
        type : "POST",
        data : {prod_id : select_id, presenkort : presentkort_type}
    });

and check that select_id is coming or not by alerting it

1 Comment

thank you gautam for your reply. Now it reloads the page but POST is still empty.
0

I had a similar issue but it was down to the max_input_vars setting in the php.ini file. I found I was sending a lot more than 1000 variables - upped it to 5000 and it worked.

Comments

0

remove DATATYPE as html and submit full form.

$j.ajax({
      url: '/cashregister/',
      type:'POST', 
      data:$j('#Id').serialize(),
      success:function(msg) {
      alert(msg);
      }

Note: #Id : form id

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.