473,796 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

bugboy
160 New Member
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.ph p" 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="findwor d.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_arr ay ($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.ph p".. 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
7 4619
bergy
89 New Member
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="findwor d.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($q uery);

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

mysql_close();
echo $output_string;

?>
[/PHP]
Sep 21 '07 #2
bugboy
160 New Member
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.p hp" 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
89 New Member
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("Locatio n: newword.php?new word=".$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
160 New Member
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><t itle>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_arr ay ($result, MYSQL_BOTH)){
$while_went = true;
$output_string .= '<tr><td>' . $row['word'] . '</td></tr></body></html>';
}
}
if(!$while_went ){
header("Locatio n: newword.php");
exit();

}

mysql_close();
echo $output_string;
echo $word
?>[/PHP]
Sep 21 '07 #5
bugboy
160 New Member
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
89 New Member
No problem, let me know if the redirect/textbox populate works for you as well.
Sep 21 '07 #7
bugboy
160 New Member
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

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

Similar topics

4
6754
by: David Walker | last post by:
Hi I have a Microsoft SQL database I can use (also mySQL, so if you know how to do this in mySQL that is just as useful). The database can only be accessed from webpages hosted on the same server as the SQL database because of firewalling. The database admin doesn't mind me accessing it like this if I can do it, and they would allow me if the firewall could be changed (which it can't / won't be). However, I need to get direct access to...
13
2893
by: dogu | last post by:
Noob alert. Code is below. File is saved as a .php. What I'm trying to do: User uses 'select' box drop down list to pick a value. Value ($site) is derived from a db query. This works fine. Value selected is used as the 'where' clause of the 2nd query. If $site is a single word, the 2nd query works like a charm. If $site is more than one word (has spaces), the query returns a null
1
3355
by: Steve | last post by:
I just spent waaaaaaaaaaaayy too much time trying to track down an error that was incorrectly reported just now, and I would like to see if someone can explain to me why it was reported that way. The purpose of the code is simply to delete a record and then redirect back to the page where the delete was started. The code looks like this: elseif ($_GET == "delete") { $query = "delete from product_subcategory2 where product_sku=$_GET and...
25
837
by: J.A. | last post by:
Hi, I'm new with PHP I want to create a page to redirect links, for example: http://www.php.net/link.php?link_id=2005 ....and be redirect to a URL, from my databse (MySQL), like it follow: http://www.php.net/pro/beta/etc/etc/index.php I have this, but did not work, what can I do?
5
2971
by: Simon | last post by:
Hi, I have a Login.php page that logs the user in and out. I has two forms within the page, (depending on what we are trying to do), either one to log in or out. The form calls itself using a post method and either logs the user in our out given the information from the form. but every pages use sessions and cookies, if the user is successfully logged in then the cookies and session values are updated, (as well as MySQL).
4
5392
by: Marty U. | last post by:
I have a Session variable I need to check the value of. If it is value a then redirect to some page. I need to implement this in a user control that is on all the relevent pages. I placed the if and redirect statement within the page_load of the user control. However, when you try to access any page that has the user control it simply loads for minutes and never actually redirects. What am I missing with executing the response.redirect...
3
2844
by: iht | last post by:
Say I have a database with types of car driven by people living in different cities. I made several queries to separate out the database according to city, then made queries to count out numbers of cars. So what I have now are queries those tally different car types in each city (simplified version): LAQuery 50 5 10 15 20 NYQuery
8
2449
siridyal
by: siridyal | last post by:
I have a wholesale website that i'm working on that shows hundreds of items that are updated from time to time. These items are kept in a mysql database with several tables. I want to let the the customer browse (from a dynamically created drop down list - which i've done) the items by furniture class, and then have the query results limited to 26 items per page, in two columns using DIVs. The CSS for the DIVs is taken care of, as is the...
18
2677
by: Paul Lautman | last post by:
JRough wrote: What do you mean by "redirect the output to Excel"??? Excel isn't a location, it's a spreadsheet program that some (but not all users) will have on their machine. BTW, Location: is supposed to take a fully qualified URL.
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.