jjmusicpro Posted October 5, 2007 Share Posted October 5, 2007 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/ Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 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. } Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362789 Share on other sites More sharing options...
Yesideez Posted October 5, 2007 Share Posted October 5, 2007 When using header() to redirect make sure you don't have ANYTHING sent to the browser first! Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362794 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 bluesky thanks... why i copypaste your code, i get an error looks like its missing something...maybe a " or something Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362797 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 mabye, what's the error? header("location:nf.php?groupid=" . $_GET['groupid'] . "?zipcode_entered_search=" . $_GET['zipcode_entered_search'] ."\"); Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362799 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 i think i had to add an extra " at the end to get it look right: header("location:nf.php?groupid=" . $_GET['groupid'] . "?zipcode_entered_search=" . $_GET['zipcode_entered_search'] ."\""); Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362801 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 ah yes, that backslash at the end would cancel the double quote, so you'd need a second one. good catch. Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362803 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 ok so i ran that, but it only display a 1/2 page with just images. Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362810 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 maybe we can try to get it to work on a if a zipcode i returned? while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $site = $row['redirect_url'] ; echo "<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=$site\">"; } Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362813 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 When using header() to redirect make sure you don't have ANYTHING sent to the browser first! how do you do that? Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362816 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 Instead of this posting to itself, could i just post it to another page, and then redirect to other pages from there? Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362829 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362831 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 how do i do what the previous guy said, i have to put that header() call before everything? Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362839 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362861 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 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{ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362867 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 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']; Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362873 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 ah cant get it to work,ill figure out another way i geuss Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362887 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 how do i get it to do the header function on this below? while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $site = $row['redirect_url'] ; echo "<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=$site\">"; } Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362895 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 you wouldn't want to header() more than once, so no need for a loop: $row = mysql_fetch_array($result, MYSQL_ASSOC); $site = $row['redirect_url'] ; header("location:$site"); exit; Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362907 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 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{ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362921 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 when i put in the redirect if 1 url is found, it give blank page, but when i put back in the echo statment for it, it work.....!?!?!?! Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362923 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 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{}?> Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362928 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 if i put your code in above, it wont work $row = mysql_fetch_array($result, MYSQL_ASSOC); $site = $row['redirect_url'] ; header("location:$site"); exit; Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362934 Share on other sites More sharing options...
jjmusicpro Posted October 5, 2007 Author Share Posted October 5, 2007 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'] ."\">";} } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362938 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 i'd look at $site using echo instead of header to make sure we have a valid location: echo("location:$site"); exit; Quote Link to comment https://forums.phpfreaks.com/topic/72012-making-a-redirect-faster-when-posting-to-same-page/#findComment-362940 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.