PDA

Click to See Complete Forum and Search --> : How to search results, and post data


joefox
Jul 10th, 2007, 12:48 PM
I wanted to make a text box, and the user put data into it, hit search, and have it post back to the page the data it found, i cant figure it out, any help?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php
require_once ('connect.php');
require_once ('opendb.php');
$query = "SELECT * FROM search_data";
$result = @mysql_query ($query);
$count = mysql_num_rows($result); //number of results

if ($count > 0){
echo "found $count results.<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo ' <tr><td align="left">' . $row['zipcode'] . '</td><td align="left">' . $row['redirect_url'] . '</td></tr>';
}
}else{
echo "No results found";
}
?>

</body>
</html>

joefox
Jul 10th, 2007, 01:05 PM
Ok so looked around and got a few bits of code working however, i cant seem to get this working.

I dont know how to post the value that was imputted by the client, and have it post its value in the search script.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form action="index.php" method="post">
Enter Zip Code: <input type="text" name="zipcode_entered_search" /><br />
<input type="submit" />
</form>

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){

echo $_POST["name"];

}else{

$sql = "SELECT * FROM search_data WHERE zipcode='test'";
$query = mysql_query($sql);
$num = mysql_num_rows($query); //HERE IS WHERE IT GETS THE COUNT
if ($num == "1") {
$row = mysql_fetch_assoc($query);
$info1 = $row['zipcode'];
$info2 = $row['redirect_url'];
$info3 = $row['notes_1'];
//if you repeat this for each column you need, you have it all sorted out in variables.
} else {
//IT IS GREATER THEN, OR LESS THEN 1
header("Location: redirect.php");
}
}
?>
</body>
</html>

asterix299
Jul 10th, 2007, 01:52 PM
Just check to see if the $_POST is set rather than using the if statement at the top.

i.e.

if (isset($_POST['name'])) {
echo $_POST['name'];
}

joefox
Jul 10th, 2007, 01:54 PM
Just check to see if the $_POST is set rather than using the if statement at the top.

i did try that, but it didnt put the value of the person typed in, into the query.

asterix299
Jul 10th, 2007, 01:56 PM
because you didn't tell it to... at least not in the code you posted.

joefox
Jul 10th, 2007, 02:04 PM
here is what i have so far, let me know if i am missing something..

I cant seem to get it to work..

i just want a form that they put in a zip code into a box, and hit submit, and it searches the db, and puts the value below it, thats it lol

thanks again for your help.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="index.php" method="post">
Enter Zip Code: <input type="text" name="zipcode_entered_search" /><br />
<input type="submit" />
</form>

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){

$_POST["zipcode_entered_search"];

$sql = "SELECT * FROM search_data WHERE zipcode='zipcode_entered_search'";
$query = mysql_query($sql);
$num = mysql_num_rows($query); //HERE IS WHERE IT GETS THE COUNT
$row = mysql_fetch_assoc($query);
$info1 = $row['zipcode'];
$info2 = $row['redirect_url'];
$info3 = $row['notes_1'];

}else{
// nothing here yet.

}
}
?>
</body>
</html>

asterix299
Jul 10th, 2007, 02:13 PM
ok...

First, get rid of:

if($_SERVER['REQUEST_METHOD'] == "POST"){

and replace it with:

if(isset($_POST['entered_zipcode'])) {

That's a little simpler to look at.

Next, you need to assign the value of $_POST['entered_zipcode'] to a variable and put that variable into the query. Right now you have your mysql query searching for the text 'zipcode_entered_search'.

To fix this simply do the following:

$zipcode = $_POST['zipcode_entered_search'];

$sql = "SELECT * FROM search_data WHERE zipcode='$zipcode'";

Before you continue with php, I suggest you read a few basic tutorials about get/post variables.

joefox
Jul 10th, 2007, 02:31 PM
zipcode_entered_search is the name of my textbox that the person puts the value in.

asterix299
Jul 10th, 2007, 02:52 PM
I realized...

What I'm saying is you didn't pass a variable into the query, you had it search for the string 'zipcode_entered_search' and not the zipcode the person entered.

joefox
Jul 10th, 2007, 03:12 PM
i got ya thanks!

Is there way to have the page redirected if only 1 result is found?

dclamp
Jul 10th, 2007, 03:27 PM
most of these questions were already answered in the last post you opend ;)


$num = mysql_num_rows(query);
if($num=="1") {
header("Location: http://www.yoursite.com");
}

joefox
Jul 10th, 2007, 03:35 PM
thanks for all the help i am new at this.

Is there a way to check for 3 things...
check for 0 results and diplay "nothing found"
if 1 result, do a redirect

and if more then 1 result, then display like i have been?

dclamp
Jul 10th, 2007, 03:48 PM
if($num=="0"){
echo "Nothing Found!";
}else if($num=="1"){
header("Location: mypage.php");
}else{
//Echo all records here
}

joefox
Jul 10th, 2007, 05:30 PM
if($num=="0"){
echo "Nothing Found!";
}else if($num=="1"){
header("Location: mypage.php");
}else{
//Echo all records here
}



Thank you very much, ok so if i get 1 result, how do i put the value from the database int he redirect?

So if i had 1 matching result, how do i put URL_REDIRECT in that so it goes there?

joefox
Jul 10th, 2007, 05:41 PM
I tried your example but it just game me a blank page..here i what i have that works, but when i try to put extra if else's in, it just goes blank for me.

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
require_once ('connect.php');
require_once ('opendb.php');

$zipcode = $_POST['zipcode_entered_search'];

$query = "SELECT * FROM search_data WHERE zipcode='$zipcode'";
$result = @mysql_query ($query);
$count = mysql_num_rows($result); //number of results

if ($count > 0){
echo "Located $count result(s).<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['zipcode'] . '<br>' . $row['company_name'] . '<br>' . $row['redirect_url'] . '<br>' . $row['notes_1'] . '<p>';
}
}else{
echo "No results found";
}

}else{
// nothing
}
?>

joefox
Jul 10th, 2007, 05:49 PM
I tried this also, and it didnt work, just comes up blank page.

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
require_once ('connect.php');
require_once ('opendb.php');

$zipcode = $_POST['zipcode_entered_search'];

$query = "SELECT * FROM search_data WHERE zipcode='$zipcode'";
$result = @mysql_query ($query);
$count = mysql_num_rows($result); //number of results

if ($count == 0){
echo "Nothing";
}
}else if($count == 1){
echo "REDIRECT";
}
}else{
echo "Located $count result(s).<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['zipcode'] . '<br>' . $row['company_name'] . '<br>' . $row['redirect_url'] . '<br>' . $row['notes_1'] . '<p>';
} } }
?>

dclamp
Jul 10th, 2007, 05:55 PM
take away the "@" sign in front of the query

joefox
Jul 10th, 2007, 06:04 PM
take away the "@" sign in front of the query

I did, i just get a blank page.

asterix299
Jul 11th, 2007, 09:24 AM
Your if statement is completely jumbled up... You're putting two closing braces at each else/elseif statement which is causing it to not work.

<?php
if(isset($_POST['zipcode_entered_search'])){
require_once ('connect.php');
require_once ('opendb.php');

$zipcode = $_POST['zipcode_entered_search'];

$query = "SELECT * FROM search_data WHERE zipcode='$zipcode'";
$result = mysql_query ($query);
$count = mysql_num_rows($result); //number of results

if ($count == 0){
echo "Nothing";
} else if($count == 1){
echo "REDIRECT";
} else{
echo "Located $count result(s).<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['zipcode'] . '<br>' . $row['company_name'] . '<br>' . $row['redirect_url'] . '<br>' . $row['notes_1'] . '<p>';
} }
?>

joefox
Jul 11th, 2007, 04:33 PM
Your if statement is completely jumbled up... You're putting two closing braces at each else/elseif statement which is causing it to not work.

<?php
if(isset($_POST['zipcode_entered_search'])){
require_once ('connect.php');
require_once ('opendb.php');

$zipcode = $_POST['zipcode_entered_search'];

$query = "SELECT * FROM search_data WHERE zipcode='$zipcode'";
$result = mysql_query ($query);
$count = mysql_num_rows($result); //number of results

if ($count == 0){
echo "Nothing";
} else if($count == 1){
echo "REDIRECT";
} else{
echo "Located $count result(s).<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['zipcode'] . '<br>' . $row['company_name'] . '<br>' . $row['redirect_url'] . '<br>' . $row['notes_1'] . '<p>';
} }
?>


I tried that, still a blank page.

dclamp
Jul 11th, 2007, 05:50 PM
try this code. If there is nothing being searched "Please Use Search Form Above!" will display


<form action="#" method="POST">
<input type="text" name="zipcode">
<input type="submit" name="Search">
</form>
<br><br><br>
<?php
if(isset($_POST['zipcode'])){
require_once ('connect.php');
require_once ('opendb.php');

$zipcode = $_POST['zipcode'];

$query = "SELECT * FROM search_data WHERE zipcode='$zipcode'";
$result = mysql_query ($query);
$count = mysql_num_rows($result); //number of results

if ($count == 0){
echo "Nothing";
} else if($count == 1){
echo "REDIRECT";
} else{
echo "Located $count result(s).<br /><br />\n";
while($row = mysql_fetch_array($result)){
echo "<p>" . $row['zipcode'] . '<br>' . $row['company_name'] . '<br>' . $row['redirect_url'] . '<br>' . $row['notes_1'] . '</p>';
}
}
} else {
echo "Please Use Search Form Above!";
}
?>

joefox
Jul 12th, 2007, 02:05 PM
if($num=="0"){
echo "Nothing Found!";
}else if($num=="1"){
header("Location: mypage.php");
}else{
//Echo all records here
}


on the refer redirect, can i redirect them to a http://www. address?

dclamp
Jul 12th, 2007, 07:46 PM
yes. header("Location: http://www.mysite.com");