Jump to content

Help - Search function with checkbox option in PHP and MySQL


Frederik

Recommended Posts

Hello

 

I’m trying to make a function using PHP and MySQL.

The thing I’m trying to make is a search function, and the availability to select some of the search results and send them an email.

 

I think I got an idea of making the search post-form, and the code for selecting the right data in the MySQL database, but I’m not sure how I can add a “Checkbox” to the search result where it should be possible to select some subjects.

 

For example it could be like this:

Result of search:

#1 Firstname1 Lastname1, age 22, email1@email.com

#2 Firstname2 Lastname2, age 25, email2@email.com

#3 Firstname3 Lastname3, age 21, email3@email.com

#4 Firstname4 Lastname4, age 22, email4@email.com

 

Then I want the opportunity to select for example #1 and #3 and then send them an email. Is this possible?

 

The code I have thought of for now is:

 

search.php:

<form action="searchresult.php" method="post">
<div class="left1"><label>Name:</label><input type="text" name="name"></div>
<div class="right"><label>Age:</label><input type="text" name="age"></div>
<div class="right"><input type="submit" name="search" value="Søg"></div>
</form>

 

Searchresult.php:

<?php
require("config.php");
$search = $_POST['search'];
if(empty($search)) {
  echo "<br><a href='search.php'>";
  echo "Error</a>\n";
}
else {

  $fejl = false;
  foreach ($_POST as $nam => $val){
    if (empty($nam)){
      $fejl = true;
      break;
    }
  }
  if ($fejl){
    echo "<font color='red'><b>Error!</b></font>";
    exit;
  }
  

  $name = $_POST['name'];
  $age = $_POST['age'];

  mysql_connect($mysql_host, $mysql_user, $mysql_pw);
  mysql_select_db($mysql_db);
  

  $sel = "SELECT * FROM testuser ";
  $sel .= "WHERE ((name = '$name' ";
  $sel .= "OR age = '$age' ";
  $sel .= "ORDER BY id ";
  $query = mysql_query($sel) or die(mysql_error());

  if (mysql_num_rows($query) == 0){
    echo ("No match");
exit;
  }
  else {
    $to_str = "";
    $navn_arr = $mail_arr = array();
    while($row = mysql_fetch_assoc($query)) {
      $navn = $row['firstname'] . " " . $row['lastname'];
      $mail = $row['email'];

    echo "<div id='searchresult'>".$row['firstname']." ".$row['lastname'].", ".$row['email'].", ".$row['age']."</div>\n";

    }
  }
}

?>

 

 

I hope that you can help me solving this problem!

 

 

Thanks in advance

 

 

- Frederik

 

Link to comment
Share on other sites

You could just add a checkbox at the start using the id as the name. Send the whole form to a script to process the data and send out the relavent emails based on which id's it recieved...

 

<?php
require("config.php");
$search = $_POST['search'];
if(empty($search)) {
  echo "<br><a href='search.php'>";
  echo "Error</a>\n";
}
else {

  $fejl = false;
  foreach ($_POST as $nam => $val){
    if (empty($nam)){
      $fejl = true;
      break;
    }
  }
  if ($fejl){
    echo "<font color='red'><b>Error!</b></font>";
    exit;
  }
  

  $name = $_POST['name'];
  $age = $_POST['age'];

  mysql_connect($mysql_host, $mysql_user, $mysql_pw);
  mysql_select_db($mysql_db);
  

  $sel = "SELECT * FROM testuser ";
  $sel .= "WHERE ((name = '$name' ";
  $sel .= "OR age = '$age' ";
  $sel .= "ORDER BY id ";
  $query = mysql_query($sel) or die(mysql_error());

  if (mysql_num_rows($query) == 0){
    echo ("No match");
exit;
  }

////modified from here onward\\\\\\\

else{
	$to_str = "";
	$navn_arr = $mail_arr = array();

echo <<<_END
<div id='searchresult'>
	<form action="emailSender.php" method="post">
_END;

	while($row = mysql_fetch_assoc($query)) 
		{
			$navn = $row['firstname'] . " " . $row['lastname'];
			$mail = $row['email'];

echo <<<_END
	<input type="checkbox" name="$row['id']"> $navn $row['email'], $mail <br/>
_END;
		}
echo <<<_END
	</form>
</div>
_END;
  }
}

?>

Link to comment
Share on other sites

That sounds good, but i'm relatively new to php, so is it possible for you to give an example of code that should be used in the file "emailsender.php"

 

 

The code, that i'm using to send emails is:


      $navn_arr [] = $navn;
      $mail_arr [] = $mail;

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25);

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Subject')
  ->setFrom(array('noreply@testmail.com' => 'Domain.com'))
  ->setBody('Message')
  ;

//Send the message
$failedRecipients = array();
$numSent = 0;

$to = array_combine($mail_arr, $navn_arr);

foreach ($to as $address => $name)
{
  $message->setTo(array($address => $name));
  $numSent += $mailer->send($message, $failedRecipients);
}

printf("Sent %d messages\n", $numSent);

Link to comment
Share on other sites

Sorry i'm not familiar with Swift_Mailer

 

Also, i realised i forgot to add a submit button.

You could just add a checkbox at the start using the id as the name. Send the whole form to a script to process the data and send out the relavent emails based on which id's it recieved...

 

<?php
require("config.php");
$search = $_POST['search'];
if(empty($search)) {
  echo "<br><a href='search.php'>";
  echo "Error</a>\n";
}
else {

  $fejl = false;
  foreach ($_POST as $nam => $val){
    if (empty($nam)){
      $fejl = true;
      break;
    }
  }
  if ($fejl){
    echo "<font color='red'><b>Error!</b></font>";
    exit;
  }
  

  $name = $_POST['name'];
  $age = $_POST['age'];

  mysql_connect($mysql_host, $mysql_user, $mysql_pw);
  mysql_select_db($mysql_db);
  

  $sel = "SELECT * FROM testuser ";
  $sel .= "WHERE ((name = '$name' ";
  $sel .= "OR age = '$age' ";
  $sel .= "ORDER BY id ";
  $query = mysql_query($sel) or die(mysql_error());

  if (mysql_num_rows($query) == 0){
    echo ("No match");
   exit;
  }

////modified from here onward\\\\\\\

else{
      $to_str = "";
      $navn_arr = $mail_arr = array();
         
echo <<<_END
   <div id='searchresult'>
      <form action="emailSender.php" method="post">
_END;
         
      while($row = mysql_fetch_assoc($query)) 
         {
            $navn = $row['firstname'] . " " . $row['lastname'];
            $mail = $row['email'];
            $theId = $row['id'];
            
echo <<<_END
      <input type="checkbox" name="$theId"> $navn, $mail <br/>
_END;
         }
echo <<<_END
       <input type="submit" value="Send email"/>
      </form>
   </div>
_END;
  }
}

?>

 

Try writing a script then named "emailSender.php".

 

Tell it to print_r($_POST);

 

It should print all the id's of the tick boxes you selected. All you need to do then is with each one of the id's retrieve all the relavent data you wish to include in your email.

Link to comment
Share on other sites

I get this result:

 

Array ( [1] => on )

 

 

When I select 1 mail from the searchresult.

 

I have this code inde my emailSender.php:

<?php
require("config.php");
print_r($_POST);
?>

 

Is it possible to display the chosen id's and then use a SELECT command with MySQL and get the values of firstname, lastname and so on at this page?

Link to comment
Share on other sites

Yea unfortunately this is how the checkbox works. If you tick it, then submit the form you just get the name of the checkbox and 'on' through the post.

 

Kind of annoying really, if you wanted to continue using checkboxes you would have to write some php to know what to look for in the post, or rather what not to, and take the key and not the value from the post array. For example if your form only had the list of email checkboxes and a submit button named 'send'

$emailAddresses = array();
foreach($_POST as $key = > $value)
{
if($key != 'send' && $key == 'on')
{
//push the address into the emailAddresses array to use after the foreach loop has finished
array_push($emailAddresses,$key);
}
}

 

If you are not fixed on using checkboxes, you could use radio buttons:

<input type="radio" name="some email" value="0"/> <input type="radio" name="some email" value="1"/> Some guy in the mailing list

That way you will always get the the emails to process no matter what when looping through the $_POST array

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.