Jump to content

Recommended Posts

Ok, right now, i have it so you enter a zipcode, and if there is 1 result, it will redirect to the URL thats in the datqabse that matches that url, if there are 2 or more, it will display the data for them on a page.

 

my quetion is, if there is 1 result right now, it will take 1 second, to post to the page, then redirect, is there a way to make it do the redirect beofre it loads teh page if there is 1 result?

 

<?php
  if($_SERVER['REQUEST_METHOD'] == "POST"){ 
  $zipcode = $_POST['zipcode_entered_search']; 
  $query2 = "INSERT INTO zipcodes_searched (zipcode,time_date) VALUES ('$zipcode', NOW())";
  $result2 = mysql_query($query2); 
  $query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode' and on_off=1"; 
  $result = @mysql_query ($query); 
  $count = mysql_num_rows($result); 

if($count=="0"){ 
echo "<meta http-equiv=\"Refresh\" content=\"0;url=nf.php?groupid=" . $_GET['groupid'] . '?zipcode_entered_search=' . $_GET['zipcode_entered_search'] ."\">";
}else if($count=="1"){ 

// Site Redirect
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$site = $row['redirect_url'] ;
echo "<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=$site\">";
}

}else{ 
     echo "<p><b>Make sure you contact the office in your territory!</b><br /><br><table width=\"400\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\" valign=\"top\">"; 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
     echo '<tr><td colspan=2 valign="middle">' . $row['company_name'] . '<br>' .$row['notes_1'] . '</b></td></tr><tr><td valign=top><img src=mm_smaller.gif></td><td>' .'<b>Phone:</b>'. ' ' . $row['phone_number'] . '<br>'.'<a href="'. $row['redirect_url'] . '"> Visit Website </a>'. ' ' .'</td></tr>'; 
    }echo '</table>';
} 

  }else{ 
// nothing
  } 
?>

use header() instead of echoing a meta redirect:

 

if($count=="0"){

    header("location:nf.php?groupid=" . $_GET['groupid'] . '?zipcode_entered_search=' . $_GET['zipcode_entered_search'] ."\");

    exit; // header doesn't guarantee code termination. exit to make sure.

}

sure.

 

but the thing i like about a page posting to itself is that i handle errors on that page before going anywhere else. that way if there is an error i don't have to header() back to the first page and somehow transfer error messages there to tell the user what's wrong. on one page, we get the errors and show them. if no errors, we go to the next page.

just make sure you don't output ANYTHING to the browser before calling a header() or you'll get the "header already sent" error. so, for instance, don't do this:

 

<HTML>
<BODY>
<?
header("location:somepage.php");
exit;
?>

 

why not? because we already started sending stuff to the browser when we sent the <HTML> tag. header() will no longer work. if you might need to header(), do it before you output anything to the browser:

 

<?
// process your code here and header if necessary NOW, before the HTML
header("location:somepage.php"); // This is fine, we haven't sent anything to the browser yet.
exit;
?>
<HTML>
<BODY>
etc...

ok so i made the first page send the zipcode to a page called process.php, but the page is blank wont do anything

 

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

		 $groupid_sent = $_GET['groupid'];


	   $query = "SELECT * FROM metrogroups WHERE groupid='$groupid_sent'"; 
  $result = @mysql_query ($query); 
  $count = mysql_num_rows($result); //number of results 
  
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$groupname = $row['groupname'] ;
}
	    }else{ 
     require_once ('connect.php'); 
  			 require_once ('opendb.php'); 

		 $groupid_sent = $_GET['groupid'];

	   $query = "SELECT * FROM metrogroups WHERE groupid='$groupid_sent'"; 
  $result = @mysql_query ($query); 
  $count = mysql_num_rows($result); //number of results 
  
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$groupname = $row['groupname'] ;
}
  } 
?> 
<?php
  if($_SERVER['REQUEST_METHOD'] == "POST"){ 
  $zipcode = $_POST['zipcode_entered_search']; 
  $query2 = "INSERT INTO zipcodes_searched (zipcode,time_date) VALUES ('$zipcode', NOW())";
  $result2 = mysql_query($query2); 
  $query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode' and on_off=1"; 
  $result = @mysql_query ($query); 
  $count = mysql_num_rows($result); 

if($count=="0"){ 
header("location:nf.php?groupid=" . $_GET['groupid'] . "?zipcode_entered_search=" . $_GET['zipcode_entered_search'] ."\"");
exit;
}else if($count=="1"){ 

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$site = $row['redirect_url'] ;
echo "<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=$site\">";
}
}else{ 
     echo "<p><b>Make sure you contact the office in your territory!</b><br /><br><table width=\"400\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\" valign=\"top\">"; 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
     echo '<tr><td colspan=2 valign="middle">' . $row['company_name'] . '<br>' .$row['notes_1'] . '</b></td></tr><tr><td valign=top><img src=mm_smaller.gif></td><td>' .'<b>Phone:</b>'. ' ' . $row['phone_number'] . '<br>'.'<a href="'. $row['redirect_url'] . '"> Visit Website </a>'. ' ' .'</td></tr>'; 
    }echo '</table>';
} 
  }else{ 
  } 
?>

do you have errors turned on? do you normally see errors if the PHP doesn't compile properly?

 

regardless, i always die on mysql_errors():

 

$result = mysql_query($query) or die(mysql_error());

 

regardless, you check to see if the form was POSTed, then you use GET variable:

 

$groupid_sent = $_GET['groupid'];

 

should probably be

 

groupid_sent = $_POST['groupid'];

Ok, so now it seems to work except, for the part at the bottom where if i have 2 or more results, to go to a page called sharred. when i go there, it just dosent display anything:

 

here is the process.php

 

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){ 
$zipcode = $_POST['zipcode_entered_search']; 
$query2 = "INSERT INTO zipcodes_searched (zipcode,time_date) VALUES ('$zipcode', NOW())";
$result2 = mysql_query($query2); 
$query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode' and on_off=1"; 
$result = @mysql_query ($query); 
$count = mysql_num_rows($result); 
if($count=="0"){ 
echo "<meta http-equiv=\"Refresh\" content=\"0;url=nf.php?groupid=" . $_GET['groupid'] . '?zipcode_entered_search=' . $_GET['zipcode_entered_search'] ."\">";
}else if($count=="1"){ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$site = $row['redirect_url'] ;
header("location:$site");
}}else{ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
echo "<meta http-equiv=\"Refresh\" content=\"0;url=shared.php?groupid=" . $_GET['groupid'] . '?zipcode_entered_search=' . $_GET['zipcode_entered_search'] ."\">";
};}}else{}?>

 

here is the display code on shared.php

 

<?php
  if($_SERVER['REQUEST_METHOD'] == "POST"){ 
  $zipcode = $_POST['zipcode_entered_search']; 
  $query2 = "INSERT INTO zipcodes_searched (zipcode,time_date) VALUES ('$zipcode', NOW())";
  $result2 = mysql_query($query2); 
  $query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode' and on_off=1"; 
  $result = @mysql_query ($query); 
  $count = mysql_num_rows($result); 

if($count=="0"){ 
}else if($count=="1"){ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$site = $row['redirect_url'] ;
echo "<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=$site\">";
}
}else{ 
     echo "<p><b>Make sure you contact the office in your territory!</b><br /><br><table width=\"400\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\" valign=\"top\">"; 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
     echo '<tr><td colspan=2 valign="middle">' . $row['company_name'] . '<br>' .$row['notes_1'] . '</b></td></tr><tr><td valign=top><img src=mm_smaller.gif></td><td>' .'<b>Phone:</b>'. ' ' . $row['phone_number'] . '<br>'.'<a href="'. $row['redirect_url'] . '"> Visit Website </a>'. ' ' .'</td></tr>'; 
    }echo '</table>';
} 
  }else{ 
  } 
?>

The header() function for the url redirect dosent seem to work, if  put that code in, it just gives a blank page.

 

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){ 
$zipcode = $_POST['zipcode_entered_search']; 
$query2 = "INSERT INTO zipcodes_searched (zipcode,time_date) VALUES ('$zipcode', NOW())";
$result2 = mysql_query($query2); 
$query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode' and on_off=1"; 
$result = @mysql_query ($query); 
$count = mysql_num_rows($result); 
if($count=="0"){ 
echo "<meta http-equiv=\"Refresh\" content=\"0;url=nf.php?groupid=" . $_GET['groupid'] . '?zipcode_entered_search=' . $_GET['zipcode_entered_search'] ."\">";
}else if($count=="1"){ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$site = $row['redirect_url'] ;
echo "<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=$site\">";
}}else{ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
echo "<meta http-equiv=\"Refresh\" content=\"0;url=shared.php?groupid=" . $_GET['groupid'] . '?zipcode_entered_search=' . $_GET['zipcode_entered_search'] ."\">";
};}}else{}?>

everytime i try to use that header() function the page comes up blank.

 

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){ 
$zipcode = $_POST['zipcode_entered_search']; 
$query2 = "INSERT INTO zipcodes_searched (zipcode,time_date) VALUES ('$zipcode', NOW())";
$result2 = mysql_query($query2); 
$query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode' and on_off=1"; 
$result = @mysql_query ($query); 
$count = mysql_num_rows($result); 
if($count=="0"){ 
echo "<meta http-equiv=\"Refresh\" content=\"0;url=nf.php?groupid=" . $groupid_sent . $_GET['zipcode_entered_search'] ."\">";
}else if($count=="1"){ 
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$site = $row['redirect_url'] ;
header("location:$site");
exit;
}else{ 
echo "<meta http-equiv=\"Refresh\" content=\"0;url=shared.php?groupid=" . $groupid_sent . $_GET['zipcode_entered_search'] ."\">";}
}
}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.