56884

PHP Not Picking Up Post Data from jQuery AJAX Form Submit

Question:

I have a form with a submit button like this:

<form action="?edit-form" method="post" class="addEdit"> <input type="submit" name="delete-image" value="Delete Image"> </form>

My jQuery AJAX is:

$.ajax({ url: $('form.addEdit').attr('action'), type: $('form.addEdit').attr('method'), data: $('form.addEdit').serialize(), success: function(html) { } });

In my PHP, I am unable to pick up that $_POST['delete-image'] isset() eventhough the submit button that sent the form is named "delete-image".

if (isset($_POST['delete-image'])) { }

"delete-image" should be set since that was the submit button I clicked. Why is PHP not picking up this posted varaible from this AJAX submit?

Answer1:

In jQuery documenation here: <a href="http://api.jquery.com/serialize/" rel="nofollow">http://api.jquery.com/serialize/</a><br /> Only succesful control are serialized, please see the documentation below:

<blockquote>

Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

</blockquote>

So submit buttn won't serialize through jQuery.serialize() function. The work around is you can add hidden input, and it will serialized.

<strong>UPDATE:</strong><br /> You need to change the form submit ajax to bind the button click. and in ajax request you can add the button value manually. Below is my code that is working.

$( "#delImage" ).click(function( event ) { event.preventDefault(); $form = $(this).parent('form'); $btnid = $(this).attr('name'); $btnval = $(this).attr('value'); $.ajax({ url: $form.attr('action'), type: $form.attr('method'), data: { "btnid" : $btnid, "btnval": $btnval, "form-data": $form.serialize() }, success: function(html) { console.log(html); } }); });

Answer2:

If you don't want the page to reload when the button is been click then just use the simple way of sending ajax call:

var mydata = $('form.addEdit').serialize(); $.ajax({ type:'POST', url: 'file.php', data: mydata, dataType:'json', success: function(responseText){ // enter code here }); } });

simply having your form without those elements

<form class="addEdit"> <input type="submit" name="delete-image" value="Delete Image"> </form>

Recommend

  • Nodejs res.render function not updating html page
  • How do you remove old version of entity framework in GAC?
  • Android ICS version building contacts code on eclipse
  • SQL: Order by column, then by substring mix asc and desc
  • nginx on VirtualBox Ubuntu guest serves stale content [closed]
  • Accessing a csv file with D3 inside meteor
  • Compare variables PHP
  • Yii: any way to save the images in compressed form?
  • Encode string to match encoded form field name in PHP POST array
  • Passing parameter through “window.location.href”
  • array_search() in Session Array
  • change color of jstree node
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Selenium to click on a javascript button corresponding to a text
  • Django simple Captcha “No module named fields” error
  • Detect when Facebook like button is clicked
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • Why does access(2) check for real and not effective UID?
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Delete MySQLi record without showing the id in the URL
  • php design question - will a Helper help here?
  • How to get icons for entities from eclipse?
  • A cron job substitute?
  • json Serialization in asp
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • JaxB to read class hierarchy
  • reshape alternating columns in less time and using less memory
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?