rickbond79 Posted June 24, 2009 Share Posted June 24, 2009 Hey people! I hope you can help me, I have a script that searches a database and returns a list of results. The script works great, except if the user doesn't enter any search term. It looks like the script uses an exit; and then the rest of the html page will not load. I understand why this happens but not how to fix it. Here's the code, look for the comment "// PROBLEM HERE" Any help would be appreciated! Cheers! <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // PROBLEM HERE check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search term.</p>"; exit; } // rows to return $limit=300; // check for a search parameter if (!isset($var)) { echo "<p>We don't seem to have a search parameter!</p>"; exit; } // Build SQL Query $query = "select * from directory where name like \"%$trimmed%\" order by name"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results.</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["name"]; $address = $row["address"]; $phone = $row["phone"]; echo " <table class='results'> <tr> <td valign='top'><span class='intro'>$count.) </span></td> <td valign='top'><span class='intro'>$title</span><br /> $address<br /> Phone: $phone</td> </table> <hr size='1' style='padding-top:0; padding-bottom:0'> " ; $count++ ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/ Share on other sites More sharing options...
RussellReal Posted June 24, 2009 Share Posted June 24, 2009 duh.. coz that html is being handled by PHP, in between the php tags it just tells it to run that thru the interpreter, everything outside of the tags are still handled by php, just not evaluated.. so when you exit PHP entirely, you discard any code thereafter, whether it be in php tags or outside of them.. try using if and else statements instead of exits.. Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-862983 Share on other sites More sharing options...
Andy-H Posted June 24, 2009 Share Posted June 24, 2009 <?php // Get the search variable from URL $var = isSet($_GET['q']) ? trim($_GET['q']) : ''; // PROBLEM HERE check for an empty string and display a message. if ( empty($var) ) { echo "<p>Please enter a search term.</p>"; } // rows to return $limit=300; // Build SQL Query $query = "select * from directory where name like '%" . mysql_real_escape_string($var) . "%' order by name"; // EDIT HERE and specify your table and field names for the SQL query $numresults = mysql_query($query); $numrows = mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<p>Sorry, your search: "" . htmlEntities($var) . "" returned zero results.</p>"; } // next determine if s has been passed to script, if not use 0 if ( !isSet($s) ) { $s = 1; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . htmlEntities($var) . ""</p>"; // begin to show results set // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["name"]; $address = $row["address"]; $phone = $row["phone"]; echo " <table class='results'> <tr> <td valign='top'><span class='intro'>" . number_format($s) . ".) </span></td> <td valign='top'><span class='intro'>" . $title. "</span><br /> " . $address . "<br /> Phone: " . $phone . "</td> </table> <hr size='1' style='padding-top:0; padding-bottom:0'> " ; $s++ ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-862987 Share on other sites More sharing options...
Andy-H Posted June 24, 2009 Share Posted June 24, 2009 Sorry, few errors there. <?php // Get the search variable from URL $var = isSet($_GET['q']) ? trim($_GET['q']) : ''; // PROBLEM HERE check for an empty string and display a message. if ( empty($var) ) { echo = "<p>Please enter a search term.</p>"; } else { // rows to return $limit=300; // Build SQL Query $query = "select * from directory where name like '%" . mysql_real_escape_string($var) . "%' order by name"; // EDIT HERE and specify your table and field names for the SQL query $numresults = mysql_query($query); $numrows = mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<p>Sorry, your search: "" . htmlEntities($var) . "" returned zero results.</p>"; } else { // next determine if s has been passed to script, if not use 0 if ( !isSet($s) ) { $s = 1; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . htmlEntities($var) . ""</p>"; // begin to show results set // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["name"]; $address = $row["address"]; $phone = $row["phone"]; echo " <table class='results'> <tr> <td valign='top'><span class='intro'>" . number_format($s) . ".) </span></td> <td valign='top'><span class='intro'>" . $title. "</span><br /> " . $address . "<br /> Phone: " . $phone . "</td> </table> <hr size='1' style='padding-top:0; padding-bottom:0'> " ; $s++ ; } } }?> Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-862997 Share on other sites More sharing options...
rickbond79 Posted June 24, 2009 Author Share Posted June 24, 2009 Thanks Andy-H, Tried replacing the code, and I'm getting a blank page - nothing seems to be loading in between the body tags. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-863002 Share on other sites More sharing options...
Andy-H Posted June 24, 2009 Share Posted June 24, 2009 if ( empty($var) ) { echo = "<p>Please enter a search term.</p>"; Sorry :$ if ( empty($var) ) { echo "<p>Please enter a search term.</p>"; Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-863007 Share on other sites More sharing options...
rickbond79 Posted June 25, 2009 Author Share Posted June 25, 2009 Cheers friend, that's got it! If you're interested here is the live version: http://www.vanierbia.com/vanier-business-directory2.php Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-863016 Share on other sites More sharing options...
Andy-H Posted June 25, 2009 Share Posted June 25, 2009 Duno if you would perfer this but it would make your search much easier to use??? <?php // Get the search variable from URL $var = isSet($_GET['q']) ? trim($_GET['q']) : ''; // PROBLEM HERE check for an empty string and display a message. if ( empty($var) ) { echo "<p>Please enter a search term.</p>\n"; echo '<form name="form1" method="get" action="results.php"> <label>Business Name: <input name="q" type="text" size="40" value="' . $var . '"> </label> <input type="submit" name="search" id="search" value="Search"> </form>'; } else { // rows to return $limit=300; // Build SQL Query $query = "select * from directory where name like '%" . mysql_real_escape_string($var) . "%' order by name"; // EDIT HERE and specify your table and field names for the SQL query $numresults = mysql_query($query); $numrows = mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<p>Sorry, your search: "" . htmlEntities($var) . "" returned zero results.</p>\n"; echo '<form name="form1" method="get" action="results.php"> <label>Business Name: <input name="q" type="text" size="40" value="' . $var . '"> </label> <input type="submit" name="search" id="search" value="Search"> </form>'; } else { // next determine if s has been passed to script, if not use 0 if ( !isSet($s) ) { $s = 1; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . htmlEntities($var) . ""</p>"; // begin to show results set // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["name"]; $address = $row["address"]; $phone = $row["phone"]; echo " <table class='results'> <tr> <td valign='top'><span class='intro'>" . number_format($s) . ".) </span></td> <td valign='top'><span class='intro'>" . $title. "</span><br /> " . $address . "<br /> Phone: " . $phone . "</td> </table> <hr size='1' style='padding-top:0; padding-bottom:0'> " ; $s++ ; } } }?> Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-863019 Share on other sites More sharing options...
Andy-H Posted June 25, 2009 Share Posted June 25, 2009 Duno if you would perfer this but it would make your search much easier to use??? <?php // Get the search variable from URL $var = isSet($_GET['q']) ? trim($_GET['q']) : ''; // PROBLEM HERE check for an empty string and display a message. if ( empty($var) ) { echo "<p>Please enter a search term.</p>\n"; echo '<form name="form1" method="get" action="results.php"> <label>Business Name: <input name="q" type="text" size="40" value="' . $var . '"> </label> <input type="submit" name="search" id="search" value="Search"> </form>'; } else { // rows to return $limit=300; // Build SQL Query $query = "select * from directory where name like '%" . mysql_real_escape_string($var) . "%' order by name"; // EDIT HERE and specify your table and field names for the SQL query $numresults = mysql_query($query); $numrows = mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<p>Sorry, your search: "" . htmlEntities($var) . "" returned zero results.</p>\n"; echo '<form name="form1" method="get" action="results.php"> <label>Business Name: <input name="q" type="text" size="40" value="' . $var . '"> </label> <input type="submit" name="search" id="search" value="Search"> </form>'; } else { // next determine if s has been passed to script, if not use 0 if ( !isSet($s) ) { $s = 1; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . htmlEntities($var) . ""</p>"; // begin to show results set // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["name"]; $address = $row["address"]; $phone = $row["phone"]; echo " <table class='results'> <tr> <td valign='top'><span class='intro'>" . number_format($s) . ".) </span></td> <td valign='top'><span class='intro'>" . $title. "</span><br /> " . $address . "<br /> Phone: " . $phone . "</td> </tr> </table> <hr size='1' style='padding-top:0; padding-bottom:0'> " ; // noticed tr tag wasnt closed $s++ ; } } }?> Quote Link to comment https://forums.phpfreaks.com/topic/163568-exiting-php-script-stops-loading-of-html-page/#findComment-863037 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.