Issues in working of PHP form submit with GET data - php

I am working with PHP forms.
The opening form tag looks like this:
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
This generated the following HTML:
<form name="adv_search_form" action="index.php?&rel=common_listing&module=company&sort_option=&search_option=&show_max_row=15" method="GET">
When I submit this form, I land on a URL:
http://localhost/projectcode12may2014/ampanel/index.php?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
But what I want is that
?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
should be appended to the action URL on the landing page. Can anyone suggest any good method for doing this?
Thanks in advance.

If you are using method="get" the parameters in action are overwritten. Only what is in the from will be transmitted.
You need to add hidden fields for all these values. For example:
<input type="hidden" name="module" value="company" />

That is your from data.
You have method="get" in your form tag. This means that the form data is concatinated to the URL. Use method="post" to have send form data as a POST request and keep your URI clean.
So change this line
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
to this
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="post">
Then you can access your form data in PHP with the global $_POST var which holds an array with the submitted data.

This is a normal behavior. Using GET method discard all parameters specified directly in action attribute.
Use POST method instead or move parameters from action attribute of the form to input fields with type="hidden".

Related

How do I preserve parameters in PHP form code

I have a script which is run with a parameter (e.g. details.php?studentid=10325).
On this script I have a form with the following form code so that the form data is sent to the current script. However, what's happening is that the script is running without the parameter. How do I preserve the parameter in this form code?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Not really recommended, but you could add a hidden <input> tag with the studentid so it is sent back when you submit the form. Like so :
<input type="hidden" name="studentid" value="<?= $_GET['studentid'] ?>">
Leave the action blank, and the form will submit back to the current url
<form method="post" action="">
Keep in mind you have specified post method, so the form values will come thru in $_POST, whereas your studentid on the query string will be in $_GET['studentid'], you can work around that by using $_REQUEST['studentid'] instead, but make sure you don't have a field in the form also called studentid

Keeping Current URL after form submit PHP

Say I have e.g: (foo.com/index.php?a=1) and a form on it to submit data, but when the submit is hit the url refreshes and it gets rid of the values from the attached.
How do I make the url on my page to stay same as before even when the sumbit button is clicked.
Thanks
Like this: action="yourpage.php?var1=value1&var2=value2". You can get these with PHP like so: $var1 = $_GET['var1'].
Example:
<form method="post" action="yourpage.php?var1=value1&var2=value2">
[...]
</form>
You can do this to add existing variables into the url:
<form method="post" action="yourpage.php?var1=<?php echo $_POST['var1'];?>&var2=<?php echo $_POST['var2'];?>">
[...]
</form>
<!-- Output:
<form method="post" action="yourpage.php?var1=value1&var2=value2">
[...]
</form>
-->
Change the form action in the HTML to post. i.e.:
<form action="..." method="post">
Assuming its a get at the moment. Then set action to your URL

Adding parameters in form action when submit

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());
});

PHP_SELF and GET parameters

When I am using $_SERVER['PHP_SELF'] in a form to redirect the page to itself, such as this:
<form method = "get" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
...some html
</form>
When the user submits the form the URI doesn't show parameters:
http://example.com/page.php
instead of this (the one I'm trying to do)
http://example.com.page.php?parameter=value
resulting in, when the user refreshes the page, it does not process the GET parameters because it is not there. How can I resolve this?
You can just put an empty form action like in the following example:
<form action="" method="GET">
All GET parameters will be preserved

the url doesn't come right when I change form method from "post" to "get"

I have a form in a file called "report.inc" and after this form is filled and submitted the result is shown in a file called "report_result.inc".
form definition:
<form method="post" action="index.php?page=report_result">
When the form method is "post" all is ok.
after submitting the form, the page I get is with the right url:
mna.co.il/index.php?page=report_result
I wanted to see the whole url with all the sent parameters, so I changed the form method from "post" to "get" like this:
<form method="get" action="index.php?page=report_result">
and now after submitting the form, the page I get is with url that misses the part of "page=report_result".
it looks like this:
"http://mna.co.il/index.php?locality=%D7%91%D7%90%D7%A8&street=%D7%90%D7%91%D7%A8%D7%94%D7%9D&hNumber=55&rooms=3&area=70&ask=380000&smscode=&x=45&y=14"
while it should be like this:
"http://mna.co.il/index.php?page=report_result &locality=%D7%91%D7%90%D7%A8&street=%D7%90%D7%91%D7%A8%D7%94%D7%9D&hNumber=55&rooms=3&area=70&ask=380000&smscode=&x=45&y=14"
What am I doing wrong?
Thanks in advance for all answers.
Anna
GET forms will wipe out any query string in their action when submitted. Use <input type="hidden"> to pass the data instead.
page=report_result is a GET parameter of the query so it's overridden by the params of your form if you use the GET method.
You should add the input
<input type="hidden" name="page" value="report_result" />
in your form and remove it from the action

Categories