How to post a keyword, then 'back' but not lose the post???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How to post a keyword, then 'back' but not lose the post???

Post by simonmlewis »

Hi

For years I have used a standard <form method='post'> type form to $_POST data to a search page.

Someone then sees the results, clicks a result, and then decides to go BACK a page in the browser. But in doing that (you may know what's coming...) you are asked "RETRY?".

How do I stop it doing that, so it just goes back and there are the results? I'm sure it is something to do with the _GET, _REQUEST, _POST type query, but am a little lost.

Hope something can help with what I suspect is a very straightward answer.

Regards
Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by AbraCadaver »

Post is only required when you're taking an action with a consequence (add, update, delete, send email, etc). This is perfectly fine for retrieval and will get rid of your problem:

[text]<form method="get" action="results.php">[/text]
Then make sure to use $_GET on the results.php page.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by simonmlewis »

Fatal error: Call to undefined function getPage()
It's on line 444, which is:

Code: Select all

<?php
	getPage();
	?>
Why on earth would it have an affect on that?
The page, index.php?page=search, uses

Code: Select all

if(isset($_GET['search']))
{
    $search = $_GET['search'];
    $_SESSION['search']=$search;
} else {
    $search=$_SESSION['search'];
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by simonmlewis »

Hold on, it' sbecause it changes the URL to:
index.php?search=bulldog
Yet it's missing "page=search&menu=home".

how do I keep that url structure??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by AbraCadaver »

[text]<form method="get" action="index.php?page=search&menu=home">[/text]

Or try leaving the action blank.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by simonmlewis »

tried that:
"<form method='POST' action='index.php?page=search&menu=home' name="searchbox" onSubmit="return formCheck(this)">".
This is what caused that problem.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by AbraCadaver »

simonmlewis wrote:tried that:
"<form method='POST' action='index.php?page=search&menu=home' name="searchbox" onSubmit="return formCheck(this)">".
This is what caused that problem.
Caused what problem? The original back button problem or the new undefined function problem? What is the working form code?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by simonmlewis »

That caused the getpage problem, because "page" wasn't in the url, as it replaced it with 'search='.
I used what was suggested, but it failed and caused that error. And when I look at the page, it's obvious why it caused the error, because it changed the URL and broke the getpage query.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by simonmlewis »

My getPage() query looks for "page=" in the URL.
So when I use that GET form, it get rids of the "page=" and just has "search=" and as a result, it cause the error.

Do you somehow place "search=" into the URL manually??

How do you do it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by simonmlewis »

And the answer, after some more research.... is:

Code: Select all

<form method='GET' action='index.php' name="searchbox"  onSubmit="return formCheck(this)">
<input type='hidden' name='page' value='search'>
<input type='hidden' name='menu' value='home'>
You have to post 'page' and 'menu' thru as separate hidden fields, and tell it what to put in those fields, to grasp it in the 'GET' in the URL.

Works perfectly.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to post a keyword, then 'back' but not lose the post

Post by AbraCadaver »

simonmlewis wrote:And the answer, after some more research.... is:

Code: Select all

<form method='GET' action='index.php' name="searchbox"  onSubmit="return formCheck(this)">
<input type='hidden' name='page' value='search'>
<input type='hidden' name='menu' value='home'>
You have to post 'page' and 'menu' thru as separate hidden fields, and tell it what to put in those fields, to grasp it in the 'GET' in the URL.

Works perfectly.
You are correct, sorry... When using post, you can append a get query string to the action, but it seems when you use get you cannot.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply