1

This is exactly what the title describes, when I hit submit, and use php file to echo the result its empty.

$( "form#fileupload" ).on( "submit", function( event ) {
  event.preventDefault();
  var formData = $( 'form#fileupload' ).serialize();
  $.ajax({
    url: 'create_adgroup.php',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
      $("#footer").html(returndata);
    }
  });

  return false;
});

and the php is as such:

ECHO "PRINT POST: ".print_r($_POST);
echo "le titre: ".$_POST['title'];

any suggestions please the alert returns the serialized string and it has all the data and title is one of them.

Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
cppit
  • 4,478
  • 11
  • 43
  • 68
  • possible duplicate of [Capture all of the form's data and submit it to a PHP script - jQuery Ajax POST example](http://stackoverflow.com/questions/5004233/capture-all-of-the-forms-data-and-submit-it-to-a-php-script-jquery-ajax-post) – Dropout Nov 19 '13 at 08:29
  • To return something from `print_r` use `print_r($_POST, true)`. – Johannes Klauß Nov 19 '13 at 08:30
  • You can't easily do file uploads with Javascript like this. – sevenseacat Nov 19 '13 at 08:30
  • its not a file upload ... the id's name was from another upload form. its a regular form no uploading – cppit Nov 19 '13 at 08:32
  • Just to make sure: You checked the javascript error console? Did you add an error-function to your script in case an actual error occurred on the ajax request? – Armin Hackmann Nov 19 '13 at 08:33
  • @Dropout that didnt resolve the issue for me sir. – cppit Nov 19 '13 at 08:34
  • @ArminHackmann I have firebug open and I do receive the results of the ajax request... it displays random echos and such. but the data sent via POST is not showing – cppit Nov 19 '13 at 08:35
  • Please paste the HTML as well, the JavaScript code looks fine. Did you check the console to see of there is a script error? or watch if there is a post happening? – Clain Dsilva Nov 19 '13 at 08:36
  • @ClainDsilva the post does happen. I am using firebug. you see that there are ~15 variables being posted. but when I try to retrieve them on the php file $_POST is empty – cppit Nov 19 '13 at 08:40
  • there is an error on your PHP code --> ECHO "PRINT POST: ".print_r($_POST); is wrong ... just try print_r($_POST); without the "echo " and the string. It should show the post variables .. – Clain Dsilva Nov 19 '13 at 08:46
  • You're probably confusing your webserver with `contentType: false`, bet that's why using the more convenient `$.post` works for you. – mabi Nov 19 '13 at 09:07
  • Im looking it up. @mabi thanks ! – cppit Nov 19 '13 at 09:45

2 Answers2

1

Try with this, usually i did the ajax POST with this kind of code:

$( "form#fileupload" ).on( "submit", function( event ) {
  $.post('create_adgroup.php', $('form#fileupload').serialize(), function(data) {
    $('#footer').html(data);
  });

  event.preventDefault();  
  return false;
});
jalansesama
  • 101
  • 2
  • The question is not about getting an Ajax method, rather fixing the existing code , which is not limited to Ajax itself. Try to read the questions and comments before posting the answers. – Clain Dsilva Nov 19 '13 at 08:55
  • this actually did work for me thank you Jalansesama! now all the vars are there and work shall continue. – cppit Nov 19 '13 at 08:56
0

For uploading file using ajax I prefer using jquery.form.js plugin. Reference

karan3112
  • 1,867
  • 14
  • 20