I have a form that submits a search for blogs on my site via GET.
The form works fine, but when submitted, the url on the following page reads ("Find it" is the value of my submit button):
www.example.com/search.php?sub_q=Find+it%21&q=tennis
This just isn't an aesthetically pleasing url. Is there a simple way to exclude the submit button from the URL? I would like the url to look like:
www.example.com/search.php?q=tennis
The search.php doesn't require $_GET['sub_q'] to operate, btw.
Try not naming your button (remove the name attribute) - it shouldn't be submitted if it doesn't have n name
See my answer here: SEO/PHP: How to Convert Form-Submit URL (Get-Method) without Javascript SEO-Friendly?
Basically: redirect to an easier-to-spell-out-URI
Related
I have a search form, and the submit button looks like this:
<input type="submit" name="search_submit" value="Go"/>
I handle this form using the following php:
if (isset($_GET['search_submit'])) {
do blah
}
Works fine. But my url then includes &search_submit=Go. I do not want that to show up.
I know that to fix this, I need to remove the name attribute from my the forms input line.
But then my php no longer works and I'm not sure how to change it to handle the form. I tried changing it to:
if (isset($_GET['submit']))
and
if (isset($_GET['Go']))
But they did not work either. If anyone can help me with an answer, it would be awesome.
You cannot remove the name of the input element, as PHP would not know which value to look for. If you want to completely hide the string after the URL, use the request method POST instead of GET:
<form action='myscript.php' method='POST'>
<input type="submit" name="search_submit" value="Go"/>
</form>
Your PHP will use the following:
$_POST['search_submit']; // Instead of $_GET['search_submit'];
A good answer to when to use GET and POST can be found here.
edit: If you just want to not have the button show up in the URL, but everything else should still be there (according to your comment), you can simply remove both the value and name of the submit button.
Instead of looking for search_submit to be set, you can look for the other values:
if (isset($_GET['username'], $_GET['password'])) {
// Do your stuff here
}
If you don't want to show string in the URL, you can use the POST method. The main difference between GET and POST are listed below as :
GET:
Parameters remain in browser history because they are part of the URL
Can be bookmarked.
GET method should not be used when sending
passwords or other sensitive information.
7607 character maximum
size.
Url example: new.php?category=sport
POST:
Parameters are not saved in browser history.
Can not be bookmarked.
POST method used when sending passwords or other
sensitive information.
8 Mb max size for the POST method.
URL example: new.php
Sample Code :
if (isset($_POST["search_submit"])) {
do blah
}
If the submit button doesn't have a name, then it won't be a successful control and won't appear in the submitted data at all.
Test for the presence of data from some other field in the form instead.
I have form and some fields and I want send these fields to the next page via done.php using action="#main_body".
What are the differences between these two forms?
<form id="formElem" name="formElem" action="/ifs/form/index.php" method="post">
<form id="formElem " class="ifs" method="post" action="#main_body">
The complete action of the form is the URL of the page containing the form at the time of loading the form + the hashtag. So submitting the form will load the same page, but with a ahashtag (anchor) of #man_body. This is a side effect of action attributes being realtive if not definitly given as absolute.
Please be aware, that it is browser-dependant and header-dependant wether the page will actually reload or just scroll.
in the first case you send the values of your inputs to a specific page called done.php. In the second way you're calling the same page in which you have your form (plus an hashtag)
In the second link you are calling the same page with a hashtag of "main_body". it will work something like a 'TOP' link provided in lengthy pages which scrolled back to top of the page.
a difference is here the page will scroll(or reload) to "main_body" when you submit the form.
So there's this hyperlink - it's happy being a hyperlink - it does not want to change to a button or a form element - it wants to stay a link!
But it would really help me if I could submit it via GET or POST (something which I switch on my pages due to design criteria). Is there ANY way that I can do this
thanks
Giles
You're in luck... clicking a hyperlink already does a GET request.
If you want to add query parameters, append them to the query string like so:
link text
Assuming you already have a form that you want to submit, you can use JavaScript to make the link submit the form:
<form id="myform">
...
</form>
Submit
As you do not want to go for form elements, there is no need of using POST method.
Simply, what you can do is:
Link
And in the page_location page,
<?php
$foo = $_GET['foo']; //here, assigns $foo = bar
// required actions
?>
Hope you get the solution! :)
I'll assume you're not really using the link as a link... that you want to submit a form with it. You'll want to keep the link inside the form you want to submit, and you can do this (jQuery):
$('#link').parents('form').attr('method', 'GET').submit();
or
$('#link').parents('form').attr('method', 'POST').submit();
im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though
I've got a CakePHP search form that has 'type'=>'get'. Basically, one of the elements in the form is a submit button of type image. When the form is posted, in the URL I always get these x & y coordinates of the image submit button:
http://site.com/controller/action?x=22&y=36&query=hello
Is there any way I can prevent the coordinates from showing up in the URL? The reason is so that someone else could use the same URL to perform the same search, without that unintuitive stuff in the link.
Thanks!
You could use some javascript on the button:
document.getElementById('myImageButton').onclick = function() {
this.form.submit();
return false;
};
Alternatively, in your controller in the beforeFilter function, you could check for the presence of the unwanted variables, strip them out and redirect to the nice URL. This does mean there'll be 2 HTTP requests made though.
Sounds like you are looking to do a Post/Redirect/Get.
Here are two examples of doing this in CakePHP:
Searching on surname
Searching on multiple fields
Advantages of redirecting a POST to a GET request are:
Users don't get the "Do you want to resubmit?" dialog if they refresh
The resulting page/query can be bookmarked
You can utilise CakePHP's built-in SEF routing, so instead of URLs with /search?q=contact you can get /search/contact
Instead of using submit helper function, use button function and set button type to submit.
echo $this->Form->**button**($this->Html->image('header_search_icon.png'), array(**'type'=>'submit'**));