When I use <form action="code.php?id=1" method="post"></form>, the form id is passed in the URL. But when I write the same code by replacing 'POST' by 'GET', the id is not passed to the URL.
Why?
When you submit a GET form, the values from the form are appended to the action URL, as the "query string" after the ?. Specifying an existing query string in the action attribute of such a form creates an ambiguity. Browsers don't merge the two query strings, they just throw away the old query string and build the new one based on the form.
With a POST form, there is no ambiguity: the data from the form is sent separately from the URL, so there is no need for the query string to be over-written.
However, it's probably best not to mix the two kind of parameters, so the solution is always to include your extra parameters as hidden fields, then it will work with both GET and POST forms:
<input type="hidden" name="id" value="1">
Better way is to pass id in hidden field.
<form action="code.php" method="post">
<input type="hidden" value="1" name="id" />
</form>
If your form is as below
<form action="code.php?id=1" method="post">
<input typ"text" name="username" />
<input type="submit" />
</form>
example script in code.php
<?php
print_r($_GET);
print_r($_POST);
print_r($_REQUEST);
?>
You will get form data in post array and url parameters in get array, in request you will get both get and post data in one array. But if you change from post to get method your form data added with the url instead of appending. This issue is because of ambiguity. To get solution i this situation, create a hidden field in your form those you also want to send with query string.
Related
How can I add may parameters when submitting my form.
<form name="frmSearch" action="list-of-all-jobs/by-date" method="POST">
<input type="text" name="kewords" id="keywords">
</form>
I already Rewrite my url to list-of-all-jobs/by-date. I just want to add may keywords parameter in the action when submitting so that the link of the page must be site.com/list-of-all-jobs/by-date/keywords.
Post action - Appends form-data inside the body of the HTTP request.
The request parameters are added in the URL seperated by '&'.
In your case it will be
list-of-all-jobs/by-date?keywords=keys.
If you want to add many keys either provide many input fields or append the keys using using commas like
list-of-all-jobs/by-date?keywords=key1,key2`
And do the processing in the server by splitting the query string parameter by commas.
You can change the form action using jquery.
HTMl is:
<form name="frmSearch" action="list-of-all-jobs/by-date" method="POST">
<input type="text" name="kewords" id="keywords"/>
<input type="submit" id="submitaction"/>
</form>
Jquery is:
$('#submitaction').click(function(e)
{
$("form").attr('action', 'list-of-all-jobs/by-date?keywords='+$('#keywords').val());
});
Kinda strange...
I'm building a shopping cart. When the user types the quantity he wants and hits "add to cart", the <form> action should redirect them with a PHP $_SERVER['QUERY_STRING'] AND some other information (i.e. the product id, fetched in MySQL).
Here's my form, all in a PHP echo...
<?php
echo '<form method="GET" action="cart.php?'.$_SERVER['QUERY_STRING'].'&action=add&item_id='.$data->item_id.'">
<small>Quantity </small><input type="text" size="2" placeholder="1" name="add_quantity">
<input type="submit" name="add_clicked" class="button" value="Add to Cart">
</form>';
?>
Upon submission, the URL redirects to cart.php but only includes the query string, but leaves out the item id and the action=add.
Supposing I typed '2' in the quantity box, the URL looks like this cart.php?add_quantity=2 and nothing after that.
Would appreciate help!
Thanks!
When you submit a form via GET, the form data submission process will overwrite any existing query string that might be set in the address you put into the action attribute.
Use hidden form fields instead to transport your additional values.
(And as #Simon already said in his comment, go read up on what you have to do to prevent XSS when outputting data that was send from the client before.)
Submitting a form with GET will overwrite any query string you'd put in the url (I'm not sure what you wanted to do with your $_SERVER['QUERY_STRING'] though as that would give the query string used to access the page where your form is.
What you'll want to do is to use hidden input fields in your form for your action and item_id attributes.
<form method="GET" action="cart.php">
<input type="hidden" name="action" value="add"/>
<input type="hidden" name="item_id" value="<?=$data->item_id?>"/>
<small>Quantity </small><input type="text" size="2" placeholder="1" name="add_quantity">
<input type="submit" name="add_clicked" class="button" value="Add to Cart">
</form>
Upon submission this will go to the url cart.php?action=add&item_id=1234&add_quantity=2
Alternatively you could (and most likely should) submit the form via POST; then any data in the form will be sent as POST parameters and the query string parameters defined in your action will be kept.
Pass the info in the query strings via a hidden field. So let's assume you're passing the account number in the query string, it would look like this:
<input type="hidden" name="account_number" value="$account_number">
I have a form on a page with get parameters:
index.php?PageID=12
I then have a multiple forms on that page which build up the page details as the user selects the details.
My problem is when the form is posted the Get overwrites other get parameters.
I can use post but then can only post the information back once as the post values are wiped when the next form is submitted;
the idea is the forms build up a address as such;
?PageID=12
?PageID=12&Section=48
?PageID=12&Section=48&Event=1456
and so on as the user selects more items.
Thanks for your help.
For forms with method=get the query string parameters specified in action attribute are ignored. Add such parameters as hidden form fields:
<form action="index.php" method="get">
<input type="hidden" name="PageID" value="12">
<input type="hidden" name="Section" value="48">
<input type="hidden" name="Event" value="1456">
</form>
You can use server-side script or JavaScript to add the query string parameters as hidden form fields.
Put the incoming $_GET params in hidden fields
you can use the below code in which you can initialize the parameter that already required to post
<form action="index.php" method="get">
Here all the parameter will join with index.php?.......
So if you required to pass some parameter by default then you can write index.php?para=1......
But don not leave it blank form action value, by default it will consider the same url that is in address bar.
May this will help you .........:)
Near the top of my page, I have this:
<?php $id = $_GET['id']; ?>
Then I have some form check conditionals that read from POST:
if (isset($_POST['completeSubmit'])) {
//code
}
And finally, I have an HTML form which looks like this:
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"; ?>" name="complete" method="post">
<input type="submit" id="textButton" name="completeSubmit" value="[mark as complete]">
</form>
The page is initially accessed by using GET with an id variable like this:
http://website.com/page.php?id=1
All subsequent form submissions (which get redirected to the same page) fail. I know you can't send both GET and POST in the same request, but seeing as my form is submitting to $_SERVER['PHP_SELF']."?id=$id" using POST shouldn't it work? This is my first time trying this so it is quite possible I've overlooked something trivial.
You can use get and post at the same time, but you shouldn't. If you want to continue to send the ID this is as simple as:
<form ...
<input type="submit" ...
<input type="hidden" name="id"
value="<?php echo htmlspecialchars($_GET['id'], ENT_QUOTES); ?>" />
</form>
Of course you can not use GET and POST methods simultaneously.
However you can use a query string while sending a form using POST method, which being used to populate $_GET array.
To find a certain error you have to provide more info. At least 2 things:
how does HTML form look
what do yo see in the query string after posting the form.
and errr...
do you use any header redirects in the form processing?
This seems so simple but I can't remember how I've done it before.
Using PHP I'm posting a form from mysite.com/?x=y and want the resulting page to be mysite.com/?x=y&formx=formy...
Options I've tried don't quite give the desired result:
action - setting action="?x=y" clears the get variables if method="get" in place of those in the form. Prior knowledge of the get variables are also required.
method - although it seems logical to set method="get", this passes the form variables but clears any placed in action. Setting method="post" retains the current get variables but doesn't add the form variables/values.
Hidden field(s) - All get variables/values can be in hidden fields with method="get". This requires prior knowledge of the get variables and a lot of duplication if there are a lot of variables or forms. This so far is the closest solution.
Just set the form's "method" attribute to "get" instead of "post".
Example:
<form action="?x=y" method="get">
<input type="text" name="query" size="20">
<input type="submit" name="submit" value="Go">
</form>
I suppose you could :
either pass those variables as <input type="hidden" name="x" vaue="y" /> in your form.
or, maybe this might work : use "mysite.com/?x=y" as action for your form : with a bit of luck, those parameters will remain when the browser will post your form -- you should try, but it might work.
Of course, if you want those parameters to appear in the URL of the destination page, you'll have to use the GET method for your form.