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
Related
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
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
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".
Let's say I have a form on my website homepage: www.mysite.com
Now, the form tag looks like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
...
</form>
When that form submits, it does everything it's suppose to. But it also reloads the page with the full filename and extension. So the user will now find the URL in the address bar is: www.mysite/index.php
Is there a way to make the form fire to the same page without adding this extension?
There may be situations where the form is as an include in a footer, so I cann't be specific about the page the form needs to fire to, hense the PHP_SELF code.
That's because $_SERVER['PHP_SELF'] refers to the actual filename of the current script.
Try this:
<form action="" method="post">
An empty action will post back to the current URL.
Try setting the action to #:
<form action="#" method="post">
...
</form>
The # refers to the current page.
Try changing action to #, this post to the current page.
Edit: Mike beat me to it.
Edit 2: It looks like you can leave out the action all together and it will default to the same page.
Edit 3: Mike beat me to that one too.
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