0

I would like to make a button link to another page. Furthermore, I would like to include some data in the url. The onclick method works, but I was trying to do it without JavaScript. The form submit method almost works, but does not send the data (id1 and id2). Any suggestions? Is it best to use JavaScript? Thank you

<input type="button" value="Clickable Button - onclick" onclick="window.location.href='http://www.google.com?id1=123&amp;id2=345'" />

<form action="http://www.google.com?id1=123&amp;id2=345"><input type="submit" value="Clickable Button - Submit" /></form>
1
  • Can you not just build the URI string in the link? Is this user submitted data? <a href="whatever.com?id=foo&something=bar">click me</a> Commented May 11, 2012 at 12:24

3 Answers 3

4

You need to add some hidden fields to your <form>:

<form action="http://www.google.com">
    <input type="hidden" name="id1" value="123" />
    <input type="hidden" name="id2" value="345" />
    <input type="submit" value="Clickable Button - Submit" />
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

Use <form action='google.com' method="get"> to force the properties to be written as url parameters. The alternative method="post" will still send the properties, but they will be hidden from the url.
GET is default for FORMS without methods iirc.
That may not be true, depending on third party frameworks/taglibs, or maybe some browsers. For example for Struts default method is post. People may want to check this if they want to make sure their url is mandatory to be formed in a specific format. Also some servers may ignore requests if they are not sent with the expected method.
2

Below is a non-js solution:

 <form method="get" action="http://www.google.com">
    <input type="hidden" name="id1" value="123" />
    <input type="hidden" name="id2" value="345" />
    <input type="submit" value="Clickable Button - Submit" />
 </form>

2 Comments

How would using Javascript be better? Using Javascript for this would actually limit your site to JS enabled browsers only. Using Javascript for this IS NOT RECOMMENDED if there is a native way in HTML.
it's a small percentage... but sites should implore progressive enhancements and not limit their audience by not allowing things to function. First; make it function without js/css, next; make it look bauce with css and js enhancements.
1
<form action="http://www.google.com">
<input type="submit" value="Clickable Button - Submit" />
<input type="hidden" name="id1" value="123" />
<input type="hidden" name="id2" value="345" />
</form>

A better alternative, though, is to use a normal link and use CSS to style it look like a button if necessary. Links Want To Be Links.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.