Muhambi
Muhambi

Reputation: 3522

On form submit, add a hidden field whether certain submit button clicked

I'm a beginning programmer who has what I thought would be a pretty simple issue. I need to pass a hidden value field to php server code through a GET request. But, I have two submits (to "view all" of the data I have on my app or to "view only the selected", and only need the hidden field value to be added only when the "View Selected" button is clicked.

Here's my code (stripped of unnecessary junk):

<form method='get' action=''> <!--the action is blank because it just submits to the same page its on-->
     <input type='hidden' name='viewselectedwashit' value='hit'/>
     <input type='submit' value='View Selected' id='viewselectedsubmit'/>
     <input type='submit' value='View All' id='viewallsubmit'/>
</form>

So basically, all I need is a way in jQuery for after "View Selected" submit button was clicked, for the hidden field to be sent with that form, but if the "View All" button was clicked, for it NOT to send the hidden form.

Thanks for any and all help! If you need more info, just ask!

Upvotes: 0

Views: 1840

Answers (2)

Dr.Molle
Dr.Molle

Reputation: 117334

There is no need for a hidden field, simply give the View Selected-button the name-attribute "viewselectedwashit".

Clicked buttons will be submitted too when they have a name-attribute.

<form>
     <input type='submit' value='View Selected' name='viewselectedwashit' />
     <input type='submit' value='View All'/>
</form>

Upvotes: 2

MrCode
MrCode

Reputation: 64526

Give the hidden an id viewselectedwashit and you can remove the element if needed. The hidden will be present only if View Selected was clicked.

$('#viewallsubmit').click(function(){
    $('#viewselectedwashit').remove();
    return true;
});

You could set it to a blank value instead using .val('').

Upvotes: 1

Related Questions