I'm working with a page where I'm trying to submit data when a link is clicked. I already know how to do this with POST and GET using javascript buttons, but how do I get the same effect when I'm working with HTML links?
You can add a query string: Something like this:
<a href="page.php?id=1&display=asdf">text</a>
This way, you get this as query string variables.
If you have a plain link you can pass them as query parameters on the link (which is a GET operation)
<a href="/myapp/somecontroller.php?id=17&name=fred">Save</a>
If you have a plain HTML form with no javascript to handle the button press you can put things in hidden fields
<form name="theform" method="post" action="/myapp/somecontroller.php">
<input type="hidden" name="id" value="17">
<input type="hidden" name="name" value="fred">
...
</form>
This doesn't require any javascript.
You can pass your data to another page using GET or POST.
Please note that when passing the data you must consider its purpose. If passing the data does not cause any changes in your application you should use GET. Otherwise, you should use POST.
If you have a user management page for instance. When you are only displaying user details you should be using GET because it is not making any sort of changes: http://www.mywebsite.com/user.php?user=10
.
However if users change their details, you should use POST because it is changing the user's data:
<form name="userDetails" method="post" action="saveChanges.php">
<input type="text" name="firstName" value="" />
<input type="submit" value="save"/>
</form>
Alternativelly, you can also use Cookies. You can save the data into a cookie that can be used throughout your web application.
I'm not totally clear on what you're asking, but this is what it sounds like:
You know how to use POST and GET, aka HTML forms, to pass data between pages. You're using Javascript buttons.
There are a couple of interpretations of "Javascript buttons":
Are you doing something like
(1) <input type="submit" onclick="doSomething();" value="Save my data" />
(2) <input type="button" onclick="doSomething();" value="Save my data" />
(3) <button onclick="doSomething();">Save my data</button>
Or something like
(4) <input type="submit" value="Save my data" />
1, 2, and 3 are examples of using javascript to make the buttons do stuff. ("Javascript buttons"). 4 is a standard HTML FORM button -- Javascript is NOT being used; the standard HTML form behaviors will cause the button to submit the data to whatever page is specified in the FORM tag (something like <form method="get" action="datacollection.php">
).
Now, the thing to understand is that POST/GET are ways to send data around between webpages -- between the browser and the server. The process of sending the data around can be triggered by HTML (an <a>
link), Javascript (a function that calls a URL or triggers that a FORM get submitted), or FORM elements like <input type="submit" />
.
I'm not intending any offense, but the way you asked your question makes me think that you're not trying to figure out different formats to transfer the data to the receiving page (as other posters discuss), and figuring out how to parse cookie values or iterate through your form fields to create a query string would be really challenging.
So here are some options:
If your form was already using Javascript to submit the form (cases 1, 2, or 3 above), you just add the same onclick part to an HTML link:
Save my data
The function submitMyForm might look something like this:
function submitMyForm() {
// Get the form object -- you can also do this with the document.form array
var myForm = document.getElementById( 'userdata' );
// use the submit method of the form object
myForm.submit();
}
So in summary, you'll add a javascript behavior ("event handler") to a normal HTML anchor link. One note -- in general, users expect the rule "links go places, buttons do things". It's not a hard and fast rule, but people generally are happier when a button is used for things like submitting form data.