I have a main page that contains two buttons. Each of the buttons should redirect to the same page, however, the page that you are redirected to should do different things depending on the button you pressed. How can I redirect the user to that new page and pass in an argument depending on which button you have clicked?
I have a main page that contains two buttons. Each of the buttons should redirect to the same page, however, the page that you are redirected to should do different things depending on the button you pressed. How can I redirect the user to that new page and pass in an argument depending on which button you have clicked?
Share Improve this question asked Oct 14, 2011 at 23:52 Saher AhwalSaher Ahwal 9,23733 gold badges90 silver badges155 bronze badges2 Answers
Reset to default 5Cross-browser event handling is much simpler with a toolkit like JQuery:
HTML:
<button data-param="foo">Foo</button>
<button data-param="bar">Bar</button>
JQuery code:
$('button').click(function(){
window.location = window.location.href + '?param=' + $(this).data('param');
});
Demo: http://jsfiddle/6DK5J/
It depends what you mean by 'pass in an argument' but if you mean by URL query string then you can simply set up a form with an action to the page in question with a GET
method:
<form action="page.html" method="GET">
<input type="submit" name="submit1" value="Submit1" />
<input type="submit" name="submit2" value="Submit2" />
</form>
The redirected URL will be page.html?submit1=Submit1
if the first button is clicked and page.html?submit2=Submit2
if the second button is clicked.