Jump to content

Exiting php script stops loading of html page


rickbond79

Recommended Posts

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++ ;

  }

?>

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

<?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++ ;
  }
?>

Link to comment
Share on other sites

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++ ;
  }
  }

  }?>

Link to comment
Share on other sites

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++ ;
  }
  }

  }?>

Link to comment
Share on other sites

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++ ;
  }
  }

  }?>

Link to comment
Share on other sites

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.