Sandeepan Nath
Sandeepan Nath

Reputation: 10294

Proper way to fix input type submit parameter not available in posted data when form submitted using jquery

In brief - When the user clicks on the form's submit button, depending on some condition, let's call it case A, I do some AJAX call, read the response and on the basis of the response, display a confirm dialogue to the user, and then when the user clicks yes, I submit the form using .submit() (not AJAX submit). In the other case, let's call it case B, I allow the form to submit. The problem is that, in the case A, the submit input parameter itself is not present in the $_REQUEST parameter in the server side. And we have some checks in the server-side code on $_REQUEST['submitform'].

Why is this so? I checked in this question that the .serialize() method does not serialize inputs of type submit, by design. Is this situation too related and by design? I am not doing any serialize.

What is the proper way to fix this? Note - I need to have both normal form submission as well as jquery form submission working in my case.

Details -

I have a form like this -

<form class="clearfix" enctype="multipart/form-data" action="/banner/addbanner/-1/2756?name=23 apr adv traffic management test&amp;action=add" method="post" id="frmBanners" name="frmBanners">
   <textarea id="comments" rows="3" cols="47" name="data[BannerInfo][comments]"></textarea>
   <input type="text" id="description" maxlength="255" size="50" value="" name="data[BannerInfo][description]">

    <input type="submit" class="stdButton save" value="Submit" name="submitform" id="submitform">
</form>

And some javascript -

$("input#submitform").click(preValidateUrls);

function preValidateUrls()
{

   if(Case A, a particular condition and URLs are not empty)
   {//Ajax call to validate some URLs

      $.ajax({ ...

      success: function(res)
      {
         if(validation fails)
         {
            if(confirm(msg))
            { //Display a javascript confirm dialogue, if user still wants to continue 
                $("form#frmBanners").submit();
            }
         }
      }
     }); //end of $.ajax

      return false; //do not allow form to get submitted before ajax response has not been received.

   }
   else
   {//case B, let the form submit normally
        return true;
   }
}

In the server side code -

if(isset($_REQUEST['submitform']) && $_REQUEST['submitform']=='Submit')
{
   //form submission detection - does not work in case A.
}

The problem is that the form submission detection does not work in the case where form submission happens using jquery, i.e. the parameter "submitform" does not exist in $_REQUEST.

Upvotes: 0

Views: 340

Answers (1)

Woutifier
Woutifier

Reputation: 313

As a quick and easy fix you can manually append "&submitform=submit" to the .serialize() output. That should do the trick.

Upvotes: 0

Related Questions