0

So I have searched around a bit in hopes of finding a solution to my problem, but have had no luck. I am basically trying to pass data into the ajax function, but when it passes it to my php file it only returns an empty array (Yes there are a few topics on this, couldn't find any to fit my needs) , here is the console output: Array ()

Its odd because just before the ajax function I log the data, and it prints out each section with no problems.The posting URL is accurate, works fine straight from my form. I have tried to use response instead of data passed through the function, but no luck their either. Thanks in advance!

Here is the JS file

$(document).ready(function() {
  $('form.ajax').on('submit', function() {

    var that = $(this),
      url = that.attr('action'),
      type = that.attr('method'),
      data = [];

    that.find('[name]').each(function(index, value) {
      var that = $(this),
        name = that.attr('name'),
        value = that.val();

      data[name] = value;
    });
    console.log(data); /////THIS LINE HERE ACTUALLY PRINTS DATA
    $.ajax({
      url: url,
      type: type,
      data: data,
      success: function(data) {
        console.log(data);
      }
    });

    return false;

  });
});

And here is my PHP

<?php //removed the issets and other checkers for ease of readability

print_r($_POST);

?>

UPDATE: I have tried to add method:"POST" to my ajax function and it still seems to be printing out blank arrays... Maybe I should convert everything to GET?

6
  • 2
    method: "POST" missing in ajax Commented Apr 29, 2016 at 7:00
  • Ahh. Tried this, and it is still printing out an empty array. Commented Apr 29, 2016 at 14:00
  • I have a theory, but it needs to be tested. At the top of your PHP file, when you put var_dump(file_get_contents(“php://input”)), what is the ouput then? Commented Apr 29, 2016 at 16:43
  • Here is the console log output: <pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i> </pre> Commented Apr 29, 2016 at 17:07
  • I believe the problem is how you are submitting your data, I don't believe you can simply post an array, try changing your ajax from data: data, to data: {data:data}, if that doesn't work, maybe try not re-using the word data so much. Commented Apr 29, 2016 at 17:13

2 Answers 2

1

jQuery ajax() uses GET as default method. You need to mention method: POST for POST requests.

method (default: 'GET')

$.ajax({
   url: url,
   method: "POST",
   type: type,
   data: data,
   success: function(data) {
       console.log(data);
   }
});

Or you can also use post().

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

1 Comment

So the keyword type is supposed to be my method. Just added method to my ajax function , along with post and it's still printing out an empty array.
0

EUREKA!!! Wow, the mistake was much simpler than I thought, figured it out solo! Thank you everyone for the tips! Finally got it

$('form.ajax').on('submit', function() {

var that = $(this),
  url = that.attr('action'),
  type = that.attr('method'),
  data = {}; // THIS NEEDS TO BE CHANGED TO BRACKETS!! 

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.