Search Results With Checkbox

needing some help :)

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1043 Views - Last Post: 03 March 2010 - 11:32 PM Rate Topic: -----

#1 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Search Results With Checkbox

Posted 02 March 2010 - 05:25 PM

I've FINALLY got my search working (yippie!). The returned results are volunteers that have signed up with my client. The only thing left that I need is:

A checkbox next to each name allowing me to give a "yes/no" option to add the volunteer to a team.

ex:

Jane Doe Add to Team? (checkbox here)

This is the code for the search:

<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
Search for: <input type="text" name="find" /> in
<Select NAME="field" id="field">
<Option VALUE="volskills">Skills</option>
<Option VALUE="txtlname">Last Name</option>
</Select>
<input type="submit" name="btnSearch" value="Search" id="btnSearch" />
<?

if (isset($_POST['btnSearch']))
{
	
  //button has been pressed, lets throw the form data into PHP variables
  	$errors=0; //used as a flag to check if there are form errors
 	$find = strtoupper($_POST['find']); //users search term
	safe($find); //the function to sanitize the data from sql injection attacks
	$find = trim($find);//trim the whitespace out
	
	//for testing
	echo "What do we have in find?  " . $find;
	
	
	$field = $_POST['field']; //what selection did they choose?
	safe($field);//the function to sanitize the data from sql injection attacks	
	$field = trim ($field);//trim the whitespace out
 
 	//for testing
	echo "What do we have in field?  " . $field;
 
 	//lcheck to see if search field is blankc

	if ($find == "")
	{
		echo "<p>You forgot to enter a search term";
		$errors = 1;
		exit;
	}


	//check see if there are no errors
	
	if($errors == 0)
	{
		//check to see if last name is being searched
		if ($field=="txtlname")
		{
			//lrun our query to search by last name
			 // echo "we are in the txtlname condition";
			  $dbhost = 'localhost';
			  $dbuser = 'softeng13';
			  $dbpass = '*****';
			  $dbname = 'softeng13';
			  
			  $db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
			  $db->setFetchMode(DB_FETCHMODE_ASSOC);
			  //$sql = "SELECT * FROM volunteers WHERE lname LIKE '$find' ORDER BY lname, fname";
			  $sql = "SELECT * FROM volunteers WHERE lname LIKE '%$find%' ORDER BY lname, fname";
			
			  $demoResult = $db->query($sql);
			  
			  echo "<h2>Search Results:</h2><br />";
			  while ($demoRow = $demoResult->fetchRow()) {
				
				
				echo $demoRow['lname'].",  " . $demoRow['fname'] . "<br/>";
							
			  }//end while			
		}//end if lname		
		
		
		if ($field=="volskills")
		{
			//lrun our query to search by skills
			 // echo "we are in the txtlname condition";
			  $dbhost = 'localhost';
			  $dbuser = 'softeng13';
			  $dbpass = '******';
			  $dbname = 'softeng13';
			  
			  $db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
			  $db->setFetchMode(DB_FETCHMODE_ASSOC);
			  //$sql = "SELECT * FROM volunteers WHERE lname LIKE '$find' ORDER BY lname, fname";
			  $sql = "SELECT volunteers.lname, volunteers.fname, skills.skillname FROM volunteers, skills, volskills
			  WHERE skills.skillname LIKE '%$find%' AND skills.skillid = volskills.skillid AND volskills.vid = volunteers.vid ORDER BY volunteers.lname, volunteers.fname";
			
			  $demoResult = $db->query($sql);
			  
			  echo "<h2>Search Results:</h2><br />";
			  while ($demoRow = $demoResult->fetchRow()) {
				
				
				echo $demoRow['lname'].",  " . $demoRow['fname'] . " | " . $demoRow['skillname'] . "<br/>";
							
			  }//end while			
		}//end if lname		
	}//end if errors == 0 
}//end if search button has been pressed
?>
</form>
</tr>
</table>



I have absolutely NO clue on how to do this. None. So any help you can offer is so appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Search Results With Checkbox

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 05:35 PM

This might be helpful:
http://www.dreaminco...wtopic86370.htm
Was This Post Helpful? 1
  • +
  • -

#3 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 05:37 PM

Thanks, Im looking at it right now. Ill let ya know :)

How will apply that to the echoed results? Is it possible?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 05:41 PM

        $output .=  "<input type = 'checkbox' ".$checked." name= ".$checkbox." >".$selection."<br>"; // Having set the variables, we set the checkbox html.


$selection would be the text.
Was This Post Helpful? 2
  • +
  • -

#5 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 05:42 PM

So I just plug in your code and set selection to my
variable?

And lets say my returned results are 11 volunteers. Will it put a checkbox next to each one giving me the option to add them to a roster (team)? Same way if there are only three results?

This post has been edited by absynthe: 02 March 2010 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 05:48 PM

Or replace it with your variable! It's been a while since I wrote that, but I don't think it would be difficult to adapt it to your code.
Was This Post Helpful? 0
  • +
  • -

#7 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 05:50 PM

Check my edit. Will it adapt with amount of returned results?

View PostCTphpnwb, on 02 March 2010 - 06:48 PM, said:

Or replace it with your variable!


Im such a PHP newbie! Sorry!
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 05:56 PM

It uses:
foreach ($checkboxname as $checkbox) {


so the number of checkboxes depends on the number of items in the $checkboxname array.
Was This Post Helpful? 1
  • +
  • -

#9 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 06:02 PM

Awesome!

Now for adding the code and adapting it. Do I need to place it at the end of my code but within the form? Like I said, Im very new to PHP and I dont want to screw up what I have. It took me awhile to get it running :)
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 06:25 PM

I'd make it into a function so that I could call it where ever I needed it. That way you can get your code organized so that when you need to make changes later it won't be difficult.
Was This Post Helpful? 0
  • +
  • -

#11 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 06:30 PM

That's a good idea! Thanks!

I have it put in but am getting these errors.
Before the search and when the page loads:

Quote

Search
Search for: in array(0) { }
Notice: Undefined index: php_self in /home/softeng13/dynamic_php/cs/admin/VolunteerSearch2.php on line 399

Notice: Undefined index: Add in /home/softeng13/dynamic_php/cs/admin/VolunteerSearch2.php on line 402

Notice: Undefined variable: checked in /home/softeng13/dynamic_php/cs/admin/VolunteerSearch2.php on line 410
Add, or any text you like.


And after I do a search:

Quote

Search

Search Results:

Doe, Jane
array(3) { ["find"]=> string(6) "Doe" ["field"]=> string(8) "txtlname" ["btnSearch"]=> string(6) "Search" }
Notice: Undefined index: php_self in /home/softeng13/dynamic_php/cs/admin/VolunteerSearch2.php on line 399

Notice: Undefined index: Add in /home/softeng13/dynamic_php/cs/admin/VolunteerSearch2.php on line 402

Notice: Undefined variable: checked in /home/softeng13/dynamic_php/cs/admin/VolunteerSearch2.php on line 410
Add, or any text you like.


Trying to figure out what Im doing wrong. Feeling lost.

This post has been edited by absynthe: 02 March 2010 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 06:46 PM

What does your code look like?
Was This Post Helpful? 0
  • +
  • -

#13 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 06:52 PM

<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
Search for: <input type="text" name="find" /> in
<Select NAME="field" id="field">
<Option VALUE="volskills">Skills</option>
<Option VALUE="txtlname">Last Name</option>
</Select>
<input type="submit" name="btnSearch" value="Search" id="btnSearch" />
<?

if (isset($_POST['btnSearch']))
{
	
  //button has been pressed, lets throw the form data into PHP variables
  	$errors=0; //used as a flag to check if there are form errors
 	$find = strtoupper($_POST['find']); //users search term
	safe($find); //the function to sanitize the data from sql injection attacks
	$find = trim($find);//trim the whitespace out
	
	//for testing
	echo "What do we have in find?  " . $find;
	
	
	$field = $_POST['field']; //what selection did they choose?
	safe($field);//the function to sanitize the data from sql injection attacks	
	$field = trim ($field);//trim the whitespace out
 
 	//for testing
	echo "What do we have in field?  " . $field;
 
 	//lcheck to see if search field is blankc

	if ($find == "")
	{
		echo "<p>You forgot to enter a search term";
		$errors = 1;
		exit;
	}


	//check see if there are no errors
	
	if($errors == 0)
	{
		//check to see if last name is being searched
		if ($field=="txtlname")
		{
			//lrun our query to search by last name
			 // echo "we are in the txtlname condition";
			  $dbhost = 'localhost';
			  $dbuser = 'softeng13';
			  $dbpass = '**';
			  $dbname = 'softeng13';
			  
			  $db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
			  $db->setFetchMode(DB_FETCHMODE_ASSOC);
			  //$sql = "SELECT * FROM volunteers WHERE lname LIKE '$find' ORDER BY lname, fname";
			  $sql = "SELECT * FROM volunteers WHERE lname LIKE '%$find%' ORDER BY lname, fname";
			
			  $demoResult = $db->query($sql);
			  
			  echo "<h2>Search Results:</h2><br />";
			  while ($demoRow = $demoResult->fetchRow()) {
				
				
				echo $demoRow['lname'].",  " . $demoRow['fname'] . "<br/>";
							
			  }//end while			
		}//end if lname		
		
		
		if ($field=="volskills")
		{
			//lrun our query to search by skills
			 // echo "we are in the txtlname condition";
			  $dbhost = 'localhost';
			  $dbuser = 'softeng13';
			  $dbpass = '**';
			  $dbname = 'softeng13';
			  
			  $db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
			  $db->setFetchMode(DB_FETCHMODE_ASSOC);
			  //$sql = "SELECT * FROM volunteers WHERE lname LIKE '$find' ORDER BY lname, fname";
			  $sql = "SELECT volunteers.lname, volunteers.fname, skills.skillname FROM volunteers, skills, volskills
			  WHERE skills.skillname LIKE '%$find%' AND skills.skillid = volskills.skillid AND volskills.vid = volunteers.vid ORDER BY volunteers.lname, volunteers.fname";
			
			  $demoResult = $db->query($sql);
			  
			  echo "<h2>Search Results:</h2><br />";
			  while ($demoRow = $demoResult->fetchRow()) {
				
				
				echo $demoRow['lname'].",  " . $demoRow['fname'] . " | " . $demoRow['skillname'] . "<br/>";
							
			  }//end while			
		}//end if lname		
	}//end if errors == 0 
}//end if search button has been pressed

$checkboxname = array("Add"); 
var_dump($_POST); // Here so that you can see the POST values, which depend on what is checked. Items unchecked will not be in POST.
$output = '';
$output .= '<form method="post" action=" '.$_SERVER['php_self'].'">
';	
foreach	($checkboxname as $checkbox) {
	if($_POST[$checkbox]=="on") 
		{
		$checked = "null"; 
		} else {
		unset($checked); 
		}
	$demoResult = $checkbox.","; 
	$output .= "<input type = 'checkbox' ".$checked." name= ".$checkbox." >".$demoResult."<br>"; // set the checkbox html.
	} // This is the end of the loop. 
$output .= '<input type="submit" name="Submit" value="Add To Roster"></form><br>'; 

echo $output;



?>
</form>

Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 07:12 PM

You haven't made it into a function. I've gotten busy tonight. I'll try to look at it again tomorrow.
Was This Post Helpful? 0
  • +
  • -

#15 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 17
  • View blog
  • Posts: 2,769
  • Joined: 20-September 08

Re: Search Results With Checkbox

Posted 02 March 2010 - 07:19 PM

Thank you so much! I look forward to tomorrow! See ya tomorrow!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2