Frederik Posted September 3, 2011 Share Posted September 3, 2011 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, [email protected] #2 Firstname2 Lastname2, age 25, [email protected] #3 Firstname3 Lastname3, age 21, [email protected] #4 Firstname4 Lastname4, age 22, [email protected] 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 Quote Link to comment https://forums.phpfreaks.com/topic/246359-help-search-function-with-checkbox-option-in-php-and-mysql/ Share on other sites More sharing options...
freelance84 Posted September 3, 2011 Share Posted September 3, 2011 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246359-help-search-function-with-checkbox-option-in-php-and-mysql/#findComment-1265130 Share on other sites More sharing options...
Frederik Posted September 3, 2011 Author Share Posted September 3, 2011 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('[email protected]' => '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); Quote Link to comment https://forums.phpfreaks.com/topic/246359-help-search-function-with-checkbox-option-in-php-and-mysql/#findComment-1265160 Share on other sites More sharing options...
freelance84 Posted September 3, 2011 Share Posted September 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246359-help-search-function-with-checkbox-option-in-php-and-mysql/#findComment-1265175 Share on other sites More sharing options...
Frederik Posted September 7, 2011 Author Share Posted September 7, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/246359-help-search-function-with-checkbox-option-in-php-and-mysql/#findComment-1266519 Share on other sites More sharing options...
freelance84 Posted September 14, 2011 Share Posted September 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246359-help-search-function-with-checkbox-option-in-php-and-mysql/#findComment-1269109 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.