Jump to content

How to return user errors on multi-input search form?


Recommended Posts

Hello all,

 

I have constructed a fairly simple search function using PHP, it can be seen here...

 

http://wesco.jp-clients.com/fasttrack_locator/new_test/

 

I have managed to get the actual SEARCH function working great. You can fill in any of those fields and get the results your looking for.

 

The problem that I'm having now tho....is how to return an error to the user if they have entered invalid information into any of the inputs. Having the multiple inputs for the form was not how it was originally. So in the beginning, I simply used this to return the error...

 

<div style="font-family:Verdana, Geneva, sans-serif;font-size:14px;padding-left:20px;">
<?php echo (count($error) > 0)?"<br /><strong>The following had errors:</strong><br />" . implode("<br />", $error) . "<br /><br />":""; ?>

<?php echo (count($results) > 0)?"<br />Results for <strong>'{$searchTerms[0]}'</strong><br /><br />" . implode("", $results):""; ?>
</div>

 

But now, having multiple inputs is making this a little more complicated.

 

Feel free to run a few searches in the form to understand what I am talking about.

 

Here is my code for the form itself (index.php)

 

<div id="form_container">
<form method="GET" action="search.php" name="searchForm" style="padding-left:20px;padding-top:7px;">
<table width="400" cellpadding="0" cellspacing="0">
<tr>
<td><label><strong>Branch Name:</strong></label></td>
<td><input type="text" size="19" name="search0" value="<?php echo isset($searchTerms[0])?$searchTerms[0]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>Branch ID:</strong></label></td>
<td><input type="text" size="19" name="search1" value="<?php echo isset($searchTerms[1])?$searchTerms[1]:''; ?>"/></input></td>
</tr>
<tr>
<td><label><strong>Address:</strong></label></td>
<td><input type="text" size="19" name="search2" value="<?php echo isset($searchTerms[2])?$searchTerms[2]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>State:</strong></label></td>
<td><input type="text" size="19" name="search3" value="<?php echo isset($searchTerms[3])?$searchTerms[3]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>Company:</strong></label></td>
<td><input type="text" size="19" name="search4" value="<?php echo isset($searchTerms[4])?$searchTerms[4]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>Region Code:</strong></label></td>
<td><input type="text" size="19" name="search5" value="<?php echo isset($searchTerms[5])?$searchTerms[5]:''; ?>" /></input></td>
</tr>
</table><br/>
<input type="Submit" id="Search" alt="Search" value="Search">
</form>
</div>

 

from there, the form passes to this page, which searches the database and returns results....

 

<?php

$h="localhost";
$u="-----";
$p="-----";
$d="-----";

$link = mysql_connect ($h, $u, $p) or die ("Could not connect to database, try again later");
mysql_select_db($d, $link);


// Set up our error check and result check array
$error = array();
$results = array();

// First check if a form was submitted. 
// Since this is a search we will use $_GET
if (isset($_GET['search0'])||isset($_GET['search1'])||isset($_GET['search2'])||isset($_GET['search3'])||isset($_GET['search4'])||isset($_GET['search5'])) {
   $searchTerms = array();
   
$searchTerms[0] = trim($_GET['search0']);
$searchTerms[1] = trim($_GET['search1']);
$searchTerms[2] = trim($_GET['search2']);
$searchTerms[3] = trim($_GET['search3']);
$searchTerms[4] = trim($_GET['search4']);
$searchTerms[5] = trim($_GET['search5']);
  
   for ($i=0; $i<=5; $i++)
   {
   $searchTerms[$i] = strip_tags($searchTerms[$i]); // remove any html/javascript.
   if (strlen($searchTerms[$i]) < 2 && strlen($searchTerms[$i]) > 0) {
   		$error[] = "<br />Search terms must be at least <strong>2</strong> characters.";
   } else {
	   	$searchTerms[$i] = mysql_real_escape_string($searchTerms[$i]); // prevent sql injection.
   }	
   }
  
  
   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT id, name, branch_id, address, state, phone, fax, company, region_code FROM markers_csv WHERE ";
      
        // grab the search types.
$types = array();
      $types[0] = "`name` LIKE '%{$searchTerms[0]}%'";
      $types[1] = "`branch_id` LIKE '%{$searchTerms[1]}%'";
      $types[2] = "`address` LIKE '%{$searchTerms[2]}%'";
  $types[3] = "`state` LIKE '%{$searchTerms[3]}%'";
  $types[4] = "`company` LIKE '%{$searchTerms[4]}%'";
      $types[5] = "`region_code` LIKE '%{$searchTerms[5]}%'";
     
      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
      
      //if (count($types) < 1)
         //$types[] = "`name` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
      
          //$andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" AND ", $types) . " ORDER BY `name`"; // order by title.

      $searchResult = mysql_query($searchSQL) or die("<br />There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
      
      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "<br />The search term '<strong>{$searchTerm}</strong>' yielded no results.";
      }else {
         $results = array(); // the result array
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "<div style='font-family:Verdana, Geneva, sans-serif;padding-bottom:20px;'>
		<div style='font-size:14px; color:#036;'>
		<strong>{$row['name']}</strong></div>
		<div style='font-size:12px; color:#000000;'>
		<strong>Branch ID:</strong> {$row['branch_id']}<br />
		<strong>Address:</strong> {$row['address']}<br />
		<strong>State:</strong> {$row['state']}<br />
		<strong>Phone:</strong> {$row['phone']}<br />
		<strong>Fax:</strong> {$row['fax']}<br />
		<strong>Company:</strong> {$row['company']}<br />
		<strong>Region Code:</strong> {$row['region_code']}</div>
		</div>";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}

?>

 

and then here is where the user errors are returned, if there are any....

 

<div id="form_container">
<form method="GET" action="search.php" name="searchForm" style="padding-left:20px;padding-top:7px;">
<table width="400" cellpadding="0" cellspacing="0">
<tr>
<td><label><strong>Branch Name:</strong></label></td>
<td><input type="text" size="19" name="search0" value="<?php echo isset($searchTerms[0])?$searchTerms[0]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>Branch ID:</strong></label></td>
<td><input type="text" size="19" name="search1" value="<?php echo isset($searchTerms[1])?$searchTerms[1]:''; ?>"/></input></td>
</tr>
<tr>
<td><label><strong>Address:</strong></label></td>
<td><input type="text" size="19" name="search2" value="<?php echo isset($searchTerms[2])?$searchTerms[2]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>State:</strong></label></td>
<td><input type="text" size="19" name="search3" value="<?php echo isset($searchTerms[3])?$searchTerms[3]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>Company:</strong></label></td>
<td><input type="text" size="19" name="search4" value="<?php echo isset($searchTerms[4])?$searchTerms[4]:''; ?>" /></input></td>
</tr>
<tr>
<td><label><strong>Region Code:</strong></label></td>
<td><input type="text" size="19" name="search5" value="<?php echo isset($searchTerms[5])?$searchTerms[5]:''; ?>" /></input></td>
</tr>
</table><br/>
<input type="Submit" id="Search" alt="Search" value="Search">
</form>
</div>


<div style="font-family:Verdana, Geneva, sans-serif;font-size:14px;padding-left:20px;">
<?php echo (count($error) > 0)?"<br /><strong>The following had errors:</strong><br />" . implode("<br />", $error) . "<br /><br />":""; ?>

<?php echo (count($results) > 0)?"<br />Results for <strong>'{$searchTerms[0]}'</strong><br /><br />" . implode("", $results):""; ?>
</div>

 

Can anybody shed some light on my problem? i'm sure it's been done before. I searched the forum before posting, but couldn't find anything that could help me with this specific problem....

 

THANK YOU IN ADVANCE!!

Not sure it'd suit what you're doing.

Maybe if you had something like..

 if(empty($_POST['searchbar'])){
$error['search'] = 'error message here';
}

if(!isset($error)){
Continue with the search and display results
}

Then just put <?php echo $error['search'] ?> anywhere on your form page, to set the error field.

You'd put that in 1 file together, not sure about multiple ones. If you use an include, then it might work.

No garuntee.

 

ok i understand what your saying and it makes sense, but i think i perhaps worded my question wrong. It's not only for the errors, but for the "results for '(whichever field you entered)' ", trying to get that to be determined based on what inputs you entered. The user is not required to fill in all fields.

you can make your errors error multidimensional. That way you can do something like

if (empty($_POST['search0'])){
//add error to errors array under the key search0
$errors['search0'][] = "You didnt enter anything in search0";
}

and then you could just do that for all the other search buttons (and of course check for other types of errors.) You could output the error messages like

if (!empty($errors)){//there were errors
foreach($errors as $key=>$errors){
//key is the search box, and errors is an error with all the errors
echo "Errors in search box $key: <br />";
echo implode("<br />", $errors);
}
}//end if

 

hmm after further review im not sure if that code snippet will help.  I have a feeling i need to manipulate this statement in some way...

 

<?php echo (count($results) > 0)?"<br />Results for <strong>'{$searchTerms[0]}'</strong><br /><br />" . implode("", $results):""; ?>

 

specifically im trying to get it so that no matter which field they enter some data into...(only at least one field is required), it will display "Results for '(whichever input they filled in to search)' "

 

i tried manipulating the statement to include $searchTerms[0], $searchTerms[1], $searchTerms[2], etc.....

 

Does this make more sense?

 

How can i manipulate the statement above? Would it be a large if/else statement?

 

Something like, "if searchTerms[0] is empty, display nothing, but if data is entered, display "results for $searchTerms[0]", and check to see if $searchTerms[1] contains any data" ...and so on and so on through all 5 fields...

 

I appreciate the responses thus far but i feel that i wasnt elaborate enough on my original post and i apologize for that.

I'm more than definately sure this isn't the problem, as it'd return errors.

I find it easier with echo's to use ' instead of "

 

<?php echo ''.(count($results) > 0).'?<br />Results for <strong>{'.$searchTerms[0].'}</strong><br /><br />'.implode("", $results):""''; ?>

It's not tested, it may or may not be what you wanted. Though you should get the drift.

According to notepad++, all of it is highlighted with syntax, some of what you posted wasn't.

displaying the "results for <strong>'{$searchTerms[0]}" isn't the problem. What i'm trying to figure out, is a code to write to make the page figure out which fields were entered, and only display "results for...." from the fields that text was entered into...

 

lets say someone entered information into search0 and search1 on the form...

 

once the search is submitted, it will display "results for (search0)" ....then skip to the next line and display "results for (search1)" .... if nothing is entered for search2 and search3 etc...then nothing is displayed because nothing was entered

i made some changes to my code.... go to the link again from above...(it is pasted again below)....

 

http://wesco.jp-clients.com/fasttrack_locator/new_test/

 

go to the FIRST input at the top, and type in 'Pittsburgh'.....

 

submit....

 

now look at what happens... got a bunch of "results for "" ". I want it to only display the "results for..." for whatever fields were entered...here's what i have tried so far...

 

<?php echo (count($results) > 0)?"<br />Results for <strong>'{$searchTerms[0]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[1]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[2]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[3]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[4]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[5]}'</strong><br /><br />" . implode("", $results):""; ?>

Ah, I see what you are doing.

//You could either have it like this
if($searchTerms[0] > 0){
echo 'Results for<strong>'.$searchTerms[0].'</strong><br />';
}elseif($searchTerms[1] > 0){
echo 'Results for<strong>'.$searchTerms[1].'</strong><br />';
}elseif($searchTerms[2] > 0){
echo 'Results for<strong>'.$searchTerms[2].'</strong><br />';
}elseif($searchTerms[3] > 0){
echo 'Results for<strong>'.$searchTerms[3].'</strong><br />';
}elseif($searchTerms[4] > 0){
echo 'Results for<strong>'.$searchTerms[4].'</strong><br />';
}elseif($searchTerms[5] > 0){
echo 'Results for<strong>'.$searchTerms[5].'</strong><br />';
};
//Or, in replacement of them echo's, you could do error formats.
$search[0] = 'Results for <strong>'.$searchTerms[0].'</strong><br />';
$search[1] = 'Results for <strong>'.$searchTerms[1].'</strong><br />';
$search[2] = 'Results for <strong>'.$searchTerms[2].'</strong><br />';
$search[3] = 'Results for <strong>'.$searchTerms[3].'</strong><br />';
$search[4] = 'Results for <strong>'.$searchTerms[4].'</strong><br />';
$search[5] = 'Results for <strong>'.$searchTerms[5].'</strong><br />';
//Then after those fields set
echo (count($results) > 0)?''.$search[0].''.$search[1].''.$search[2].''.$search[3].''.$search[4].''.$search[5].'<br />'.implode("", $results).'';

 

What this does, is check the string length is above 0, then displays a field.

If string length is below 0, display nothing.

Where the echo's would go, would be where you want them to be displayed.

 

The problem with what you had;

<?php echo (count($results) > 0)?"<br />Results for <strong>'{$searchTerms[0]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[1]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[2]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[3]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[4]}'</strong><br />" . "<br />Results for <strong>'{$searchTerms[5]}'</strong><br /><br />" . implode("", $results):""; ?>

Is that whether you had a value in the text boxes or not, it will still say "Results for ' '" As it is not connected to an IF statement to display it when values are above 0.

 

 

 

I just looked at your search form again, it seems as if the problem is sorted now.

hmm...that code is working. I slightly modified it, and im very close to getting the functionality i need. It's very cleanly and efficiently displaying the 'results from .. ' but it is still displaying the empty ones.

 

<div style="font-family:Verdana, Geneva, sans-serif;font-size:14px;padding-left:20px;">
<?php echo (count($error) > 0)?"<br /><strong>The following had errors:</strong><br />" . implode("<br />", $error) . "<br /><br />":""; ?>

<?php 
$search[0] = 'Results for <strong>'.$searchTerms[0].'</strong><br />';
$search[1] = 'Results for <strong>'.$searchTerms[1].'</strong><br />';
$search[2] = 'Results for <strong>'.$searchTerms[2].'</strong><br />';
$search[3] = 'Results for <strong>'.$searchTerms[3].'</strong><br />';
$search[4] = 'Results for <strong>'.$searchTerms[4].'</strong><br />';
$search[5] = 'Results for <strong>'.$searchTerms[5].'</strong><br />';

//echo (count($results) > 0)?"<br />Results for <strong>'{$searchTerms[0]}'</strong><br /><br />" . implode("", $results):""; 
echo (count($results) > 0)?''.$search[0].''.$search[1].''.$search[2].''.$search[3].''.$search[4].''.$search[5].'<br />'.implode("", $results):"";
?>
</div>

 

Here's a link. Search a city. Then when the results pop up, pick one and fill out all the information for it in the search box. It's picking up everything and displaying it, but theres still empty results strings up there.

 

You think perhaps an if statement would be the way to go?

 

http://wesco.jp-clients.com/fasttrack_locator/new_test/

I quoted in the code

"// Or, in replacement of those echo's, you could do error formats"

 

So where this would be

if($searchTerms[0] > 0){
echo 'Results for<strong>'.$searchTerms[0].'</strong><br />';
}elseif($searchTerms[1] > 0){
echo 'Results for<strong>'.$searchTerms[1].'</strong><br />';
}elseif($searchTerms[2] > 0){
echo 'Results for<strong>'.$searchTerms[2].'</strong><br />';
}elseif($searchTerms[3] > 0){
echo 'Results for<strong>'.$searchTerms[3].'</strong><br />';
}elseif($searchTerms[4] > 0){
echo 'Results for<strong>'.$searchTerms[4].'</strong><br />';
}elseif($searchTerms[5] > 0){
echo 'Results for<strong>'.$searchTerms[5].'</strong><br />';
};

 

Turns into

if($searchTerms[0] > 0){
$search[0] = 'Results for <strong>'.$searchTerms[0].'</strong><br />';
}elseif($searchTerms[1] > 0){
$search[1] = 'Results for <strong>'.$searchTerms[1].'</strong><br />';
}elseif($searchTerms[2] > 0){
$search[2] = 'Results for <strong>'.$searchTerms[2].'</strong><br />';
}elseif($searchTerms[3] > 0){
$search[3] = 'Results for <strong>'.$searchTerms[3].'</strong><br />';
}elseif($searchTerms[4] > 0){
$search[4] = 'Results for <strong>'.$searchTerms[4].'</strong><br />';
}elseif($searchTerms[5] > 0){
$search[5] = 'Results for <strong>'.$searchTerms[5].'</strong><br />';
};

 

As I quoted about replacing the echo's with the following. Perhaps I never made myself clear enough.

You're code should look like this.

<div style="font-family:Verdana, Geneva, sans-serif;font-size:14px;padding-left:20px;">
<?php echo (count($error) > 0)?"<br /><strong>The following had errors:</strong><br />" . implode("<br />", $error) . "<br /><br />":"";

if($searchTerms[0] > 0){
$search[0] = 'Results for <strong>'.$searchTerms[0].'</strong><br />';
}
elseif($searchTerms[1] > 0){
$search[1] = 'Results for <strong>'.$searchTerms[1].'</strong><br />';
}
elseif($searchTerms[2] > 0){
$search[2] = 'Results for <strong>'.$searchTerms[2].'</strong><br />';
}
elseif($searchTerms[3] > 0){
$search[3] = 'Results for <strong>'.$searchTerms[3].'</strong><br />';
}
elseif($searchTerms[4] > 0){
$search[4] = 'Results for <strong>'.$searchTerms[4].'</strong><br />';
}
elseif($searchTerms[5] > 0){
$search[5] = 'Results for <strong>'.$searchTerms[5].'</strong><br />';
};

//echo (count($results) > 0)?"<br />Results for <strong>'{$searchTerms[0]}'</strong><br /><br />" . implode("", $results):""; 
echo (count($results) > 0)?''.$search[0].''.$search[1].''.$search[2].''.$search[3].''.$search[4].''.$search[5].'<br />'.implode("", $results):"";
?>
</div>

 

Let me know if you have any problems.

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.