By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,403 Members | 621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,403 IT Pros & Developers. It's quick & easy.

Redirect to different pages depending on result of a form driven MySQL query.

bugboy
100+
P: 160
Hi, I'm just learning PHP. I want to redirect the user, after hitting submit, either to the same page displaying the results (as it is now) or if no results are found then send them to a new page passing on the original form data there instead.

Below is a simplified version of what i'm trying to do. The user sumits a word. If it's in the DB then the word is listed below the reset form. What i need is to redirect to a new page "newword.php" if the word is not found in the DB. The original form data should be redirected as well.

[PHP]<?php

$word = $_REQUEST['word'];

echo '<form action="findword.php" method="get">
<p><b>Find</b>
<input type="text" name="word" size="30" maxlenght="30" />
<input type="submit" name="Get" value="Go"/></p>
</form>';

require_once ('mysql_connect.php');

$query = "SELECT `word` FROM `words` WHERE `word` = '$word'";

$result = @mysql_query ($query);
//
if ($result) {

while ($row = mysql_fetch_array ($result, MYSQL_BOTH)) {
echo '<tr><td>' . $row['word'] . '</td></tr>';
}}
//This part doesn't work. i've tried all kinds of values for $result with if, else, empty() and isset()
//I want to redirect to "newword.php".. i'm just not getting it
if ($result == NULL) { // How do i forward to another page if no results?
; }

mysql_close();

?>[/PHP]

Above, i've tried to use an IF $result == NULL which of course doesn't work.. why i'm not sure.. what would $result == if nothing is returned?

Also I may need to change the form? ..so it sends the entry data to a different page if no results are found in query.

Thanks in advance for you help. The Scripts is awesome!

BugBoy
Sep 21 '07 #1
Share this Question
Share on Google+
7 Replies

bergy
P: 89
Below is a way you can accomplish exactly what you're looking for. I did have to change a few things -- first, alot of the time when I am trying to decide whether a result actually has data, I just add a quick boolean variable inside of my while loop, if there are no results, the while loop doesn't happen and my variable never gets set to true.

An easy way to do a redirect in PHP is to use the header(); function (look it up on PHP.net it can be very simple or get pretty complicated). The key with header is that it must be invoked before any output (html, text, echos, etc) are sent to the browser, and you must follow it with an exit(); function. So, I replaced your "echos" with a variable called $output_string - all of your text is tacked on to the end of that variable, and it's echoed after the header() stuff happens so it won't cause an error.

Hope this helps!! Happy Coding!!

[PHP]
<?php

$word = $_REQUEST['word'];

$output_string = '<form action="findword.php" method="get">
<p><b>Find</b>
<input type="text" name="word" size="30" maxlenght="30" />
<input type="submit" name="Get" value="Go"/></p>
</form>';

require_once ('mysql_connect.php');

$query = "SELECT `word` FROM `words` WHERE `word` = '$word'";

$result = @mysql_query($query);

if($result) {
$while_went = false;
while ($row = mysql_fetch_array ($result, MYSQL_BOTH)){
$while_went = true;
$output_string .= '<tr><td>' . $row['word'] . '</td></tr>';
}
if(!$while_went){
header("Location: newword.php");
exit();
}
}

mysql_close();
echo $output_string;

?>
[/PHP]
Sep 21 '07 #2

bugboy
100+
P: 160
This is great! you made it look easy! Thanks.

I still have a couple of unwanted behaviors. When I load the findword.php it is automatically forwarding to newword.php.. to get past this i have created a enterword.html page to start on that has no php or redirect.

I would prefer not to have three pages. Can i stop "findword.php" from forwarding immediately on loading so that i can start there?.. i guess it runs the query and finds nothing because the form starts empty...the page automatically queries on load and finds nothing triggering the forward. Can i delay the query until i've had a chance to enter a word? or Maybe i could give the form a default value that will be found in the DB? Or give the $while_went variable a counter value so the first time through counts as '1' but it won't forward untill '2'? How would i make it count? ..or is there a another way so that i don't have a useless query every time the page loads?

I enter a word on enterword.html and it sends it to findword.php, if found in the DB it stops but if it's not found it in turn forwards to newword.php but does not seem to pass on the word variable. When i type in an unknown word i would like the value to to be forwarded to the newword.php page through the 2 or 3 pages that redirect. Can a variable's value be passed from page to page to page?

If i can pass the variable to newword.php I would like the newword form to be populated with the unknown word.. How do i get a variable to pre-populate a form? if anybody can help with this i would really appreciate it!

Thanks!

BugBoy
Sep 21 '07 #3

bergy
P: 89
1) That was my fault, replace line #23 with this:
[PHP]if(!$while_went && $word != ""){[/PHP]
This way, it will only redirect if the $word has a value (meaning someone submitted a value from the form).

2) If you would like to pass a variable you can add it to the query string at the end of the php in the header function, by changing line 24 like this:
[PHP]
header("Location: newword.php?newword=".$word);
[/PHP]
Just remember you'll refer to that "newword" variable using the $_GET array so to echo it it would be:
[php]echo $_GET['newword'];[/php]
3) To populate a textbox with something you use the value attribute in the input tag... so continuing with this example, we'll add value="$_GET['newword']" something like this would work:
[php]
echo '<input type="text" name="word" size="30" maxlength="30" value="'.$_GET['newword'].'" />';
[/php]
Hope that helps, Happy Coding!
Sep 21 '07 #4

bugboy
100+
P: 160
Here is my best stab that doesn't work.. i'm trying to avoid the while loop and the subsiquent redirect when the form is still empty on initial load. I'm getting an error on the 'else' after the first IF.. when i change the else to an IF the page forwards anyway... I want it to redirect if no word is found but not redirect if the from is empty.


[PHP]<?php

$word = $_REQUEST['word'];

$output_string =
'<html><head><title>F</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body>
<form action="f.php" method="get">
<p><b>Find</b>
<input type="text" name="word" size="30" maxlenght="30" />
<input type="submit" name="Get" value="Go"/></p>
</form>';

require_once ('mysql_connect.php');

$query = "SELECT `QuantumIndex`.`words`.`word` FROM `QuantumIndex`.`words` WHERE `QuantumIndex`.`words`.`word` = '$word'";

$result = @mysql_query ($query);

if($word == NULL) { $while_went = false;
}
else($result) {
$while_went = false;
while ($row = mysql_fetch_array ($result, MYSQL_BOTH)){
$while_went = true;
$output_string .= '<tr><td>' . $row['word'] . '</td></tr></body></html>';
}
}
if(!$while_went){
header("Location: newword.php");
exit();

}

mysql_close();
echo $output_string;
echo $word
?>[/PHP]
Sep 21 '07 #5

bugboy
100+
P: 160
1) That was my fault, replace line #23 with this:
[PHP]if(!$while_went && $word != ""){[/PHP]

OH Thanks!! i was posting my stab at it when you must have be replying.. thanks so much for your time and brains!

It seems to be working exactly like i wanted!!

BugBoy
Sep 21 '07 #6

bergy
P: 89
No problem, let me know if the redirect/textbox populate works for you as well.
Sep 21 '07 #7

bugboy
100+
P: 160
Yep It's works like a charm! now i have to sit down and figure out what you've done to make it work.. Thanks again.

BugBoy
Sep 21 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.