njfuller Posted March 8, 2009 Share Posted March 8, 2009 Hi all, I have asked in several forums but have not yet found anyone who really knows how to do this. I have a simple contact form on my website, using html form + php to send the info. I would like to make a change to the form, but do not know how to do it. The change is fairly simple, instead of having the form sent automatically to "me@mycompany.com" I would like the user to be able to select via checkboxes whether they send the info to "me@mycompany.com" "you@yourcompany.com" or "us@ourcompany.com" - or if they like... all 3 I have played and played but have got utterly lost, the php script for the contact form is below, I have made no changes and it works fine as it is - but just emails "me@mycompany.com" - could anybody please please please let me know how to make these changes to allow 3 checkboxes to select who you email? I will be so grateful for any help Thank you! <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $message = $_POST['message']; $emailmessage = "Name: {$firstname} {$lastname}, with an email address of: {$email}, has contacted mycompany with the following message: {$message}"; $response = "Dear {$firstname}, Thank you for contacing mycompany, we have received your message and will respond within 24 hours. Sincerely, The Team"; mail($email, 'Your message to Mycompany',$response, "FROM:noreply@mycompany.com\r\n"); mail('me@mycompany.com', 'Message sent to mycompany', $emailmessage, "FROM:$email"); ?> Quote Link to comment Share on other sites More sharing options...
mo Posted March 8, 2009 Share Posted March 8, 2009 Checkboxes: <form .....> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"me@mycompany.com\"> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"you@mycompany.com\"> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"us@mycompany.com\"> <input type=\"hidden\" name=\"process\" value=\"1\"> <input type=\"hidden\" name=\"submit" value=\"submit\"> </form> if(isset($_POST['process'])) { foreach($_POST['ContactAddress'] as $AddrVal => $value) { send email to $value; } } Quote Link to comment Share on other sites More sharing options...
njfuller Posted March 8, 2009 Author Share Posted March 8, 2009 Thank you very much for the reply, will the mailto command remain the same? Or will I have to make a few changes elsewhere - bit of a noob I'm afraid Quote Link to comment Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 Hey please add some security to that form before some one jacks you up. Also for a good example of a mail script check this link: http://www.phpfreaks.com/forums/index.php/topic,240974.msg1129162.html#msg1129162 Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 8, 2009 Share Posted March 8, 2009 Hi Above would do it, but would mean the email addresses were on the page, and liable to be picked up by spammers. Would keep the email addresses just in the php script and use / assign them depending on which check box(es) is selected. Something liek this might better. <form .....> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"me\"> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"you\"> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"us\"> <input type=\"hidden\" name=\"process\" value=\"1\"> <input type=\"hidden\" name=\"submit" value=\"submit\"> </form> $Addreses = array("me@mycompany.com","you@mycompany.com","us@mycompany.com"); if(isset($_POST['process'])) { foreach($_POST['ContactAddress'] as $AddrVal => $value) { send email to $Addreses[$AddrVal]; } } } All the best Keith Quote Link to comment Share on other sites More sharing options...
mo Posted March 8, 2009 Share Posted March 8, 2009 Hey please add some security to that form before some one jacks you up. Also for a good example of a mail script check this link: http://www.phpfreaks.com/forums/index.php/topic,240974.msg1129162.html#msg1129162 Wolf, I was just posting a quick example of how to fulfill njfuller's requirment, not to copied as is. My real for uses htmlentities as well as a custom function that checks all user input fields for SQL injection, etc. njfuller, Please do as Wolf states and add security to all your forms and do not copy my code as is. It is just a reference. ------------------------- Next time I post a reply I will be more detailed and specific. Quote Link to comment Share on other sites More sharing options...
njfuller Posted March 8, 2009 Author Share Posted March 8, 2009 Hi all, I'm getting the error: Parse error: syntax error, unexpected T_STRING in /homepages/11/d272207702/htdocs/contactform_responsetest.php on line 128 Any ideas? 128 is: send email to $value; <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $message = $_POST['message']; if(isset($_POST['process'])) { foreach($_POST['ContactAddress'] as $AddrVal => $value) { send email to $value; } } $emailmessage = "Name: {$firstname} {$lastname}, with an email address of: {$email}, has contacted mycompany with the following message: {$message}"; $response = "Dear {$firstname}, Thank you for contacing mycompany, we have received your message and will respond within 24 hours. Sincerely, The Team"; mail($email, 'Your message to Mycompany',$response, "FROM:noreply@mycompany.com\r\n"); mail('me@mycompany.com', 'Message sent to mycompany', $emailmessage, "FROM:$email"); ?> Quote Link to comment Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 send email to is not a function... why is that line even there? Perhaps you meant: $email = $value; ???? Quote Link to comment Share on other sites More sharing options...
njfuller Posted March 8, 2009 Author Share Posted March 8, 2009 Thank you Kickstart - much appreciated. I have a fairly retared question I'm afraid, - your suggestion of: <form .....> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"me\"> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"you\"> <input name=\"ContactAddress[]\" type=\"checkbox\" value=\"us\"> <input type=\"hidden\" name=\"process\" value=\"1\"> <input type=\"hidden\" name=\"submit" value=\"submit\"> </form> I have always added checkbox values in Dreamweaver by clicking on the checkbox, rather than entering the code into the page direct. Does the below code work? As I am unsure where to enter the input types... Thank you / apologies for lack of knowledge <tr> <td> </td> <td><input name="ContactAddress" type="checkbox" id="ContactAddress" value="me" /> <label for="ContactAddress">label</label></td> </tr> <tr> <td> </td> <td><input name="ContactAddress" type="checkbox" id="ContactAddress" value="us" /> <label for="ContactAddress">label</label></td> </tr> <tr> Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 8, 2009 Share Posted March 8, 2009 Hi Think you need "[]" after each occurance of name="ContactAddress" (so should be name="ContactAddress[]". You can probably ignore the id fields (they should be unique, but probably not relevant for you to have them there unless using Javascript). To take your fragment of code:- <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $message = $_POST['message']; $Addreses = array("me@mycompany.com","you@mycompany.com","us@mycompany.com"); if(isset($_POST['process'])) { foreach($_POST['ContactAddress'] as $AddrVal => $value) { send email to $value; $emailmessage = "Name: {$firstname} {$lastname}, with an email address of: {$email}, has contacted mycompany with the following message: {$message}"; $response = "Dear {$firstname}, Thank you for contacing mycompany, we have received your message and will respond within 24 hours. Sincerely, The Team"; mail($Addreses[$AddrVal], 'Message sent to mycompany', $emailmessage, "FROM:$email"); } if ($response) mail($email, 'Your message to Mycompany',$response, "FROM:noreply@mycompany.com\r\n"); } ?> All the best Keith Quote Link to comment Share on other sites More sharing options...
njfuller Posted March 8, 2009 Author Share Posted March 8, 2009 Another forum has suggested this - it works up to a point, but only allows you to send the message to any one recipient, not all three. I'm having difficulties with the suggestions here (my fault - not any of yours) - but am veyr close to success with this. What is your opinion on the below code, and what change could I make to ensure it sends the message to ALL selected recipients? Thank you so much once again - almost there! <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $message = $_POST['message']; $chkbox1 = $_POST['chkbox1']; $chkbox2 = $_POST['chkbox2']; $chkbox3 = $_POST['chkbox3']; $sendtoemail = ""; if($chkbox1=="on"){$sendtoemail.="me@mycompany.com;";} if($chkbox2=="on"){$sendtoemail.="you@yourcompany.com;";} if($chkbox3=="on"){$sendtoemail.="us@ourcompany.com;";} $emailmessage = "Name: {$firstname} {$lastname}, with an email address of: {$email}, has contacted mycompany with the following message: {$message}"; $response = "Dear {$firstname}, Thank you for contacing mycompany, we have received your message and will respond within 24 hours. Sincerely, The Team"; mail($email, 'Your message to Mycompany',$response, "FROM:noreply@mycompany.com\r\n"); mail($sendtoemail, 'Message sent to mycompany', $emailmessage, "FROM:$email"); ?> Quote Link to comment Share on other sites More sharing options...
njfuller Posted March 8, 2009 Author Share Posted March 8, 2009 Hi everyone, Thank you all very much for your help. The code is working (using the method suggested in THIS forum!). I only have one remaining question and then I'm off. I used some of the tips from Kickstart's message above (thank you Kickstart) and my last question is this: As far as I can see there is no direct linke between the check boxes and the email addresses listed in the array. Is it simple the case that PHP is selecting the 1st email address, if the 1st checkbox is check, the 2nd if the 2nd is checked and so on? If that is the case, I will have over 50 email addresses in the final version, so I would rather have a system which is making reference top a checkbox by name (if possible) simply as this is less likely to produce mistakes.... Am I making sense? Feel free to tell me off..... Rsvp N Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 8, 2009 Share Posted March 8, 2009 Have a look at this and just add the emailing code as provided. <?php if(isset($_POST['submit'])){ if($_POST['emails']){ foreach($_POST['emails'] as $email){ echo " email sent to:\n $email <br> "; } } } $a=array("name1"=>"nam1@name1.com","name2"=>"nam2@name2.com","name3"=>"nam3@name3.com"); echo " <form method='POST' action='{$_SERVER['PHP_SELF']}'>"; foreach($a as $key=>$val){ echo " <br><input type='checkbox' name='emails[]' value='$val'>\n$key</option <br>"; } echo"<br><br> <input type='submit' name='submit' value='send email!'>"; echo "</form>"; ?> Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 8, 2009 Share Posted March 8, 2009 Hi Makes perfect sense. You could build your list of checkboxes with individual names if you are worred (ie name="chkEmail0", and just keep incrementing the number as you put them out). You could then loop through all the input fields with a [1]foreach($_POST as $field => $value)[/i] and if they are ticked then use the number as a subscript to an array. For example, this one just has an array of the email addresses, and outputs the form from the array as well as processing from the array:- <form method="PUT" > <?php $Addreses = array("me@mycompany.com","you@mycompany.com","us@mycompany.com"); $AddressNo = 0; foreach($Addresses as $Address) { echo '<input name="ContactAddress'.$AddressNo.'" type="checkbox" value="'.strstr($Address, '@', true).'">'; $AddressNo++. } ?> <input type="hidden" name="process" value="1"> <input type="hidden" name="submit" value="submit"> </form> <?php if(isset($_POST['process'])) { $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $message = $_POST['message']; foreach($_POST as $AddrVal => $value) { if (substr($AddrVal,0,14) == "ContactAddress") { $AddrNum = substr($AddrVal,14); send email to $value; $emailmessage = "Name: {$firstname} {$lastname}, with an email address of: {$email}, has contacted mycompany with the following message: {$message}"; $response = "Dear {$firstname}, Thank you for contacing mycompany, we have received your message and will respond within 24 hours. Sincerely, The Team"; mail($Addreses[$AddrNum], 'Message sent to mycompany', $emailmessage, "FROM:$email"); } } if ($response) mail($email, 'Your message to Mycompany',$response, "FROM:noreply@mycompany.com\r\n"); } ?> Or, if the list might change regularly and you don't want to use the numeric key here is a slight variation:- <form method="PUT" > <?php $Addreses = array("me" => "me@mycompany.com", "you" => "you@mycompany.com", "us" => "us@mycompany.com"); $AddressNo = 0; foreach($Addresses as $key => $Address) { echo '<input name="ContactAddress'.$AddressNo++.'" type="checkbox" value="'.$key.'">'; } ?> <input type="hidden" name="process" value="1"> <input type="hidden" name="submit" value="submit"> </form> <?php if(isset($_POST['process'])) { $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $message = $_POST['message']; foreach($_POST as $AddrVal => $value) { if (substr($AddrVal,0,14) == "ContactAddress") { send email to $value; $emailmessage = "Name: {$firstname} {$lastname}, with an email address of: {$email}, has contacted mycompany with the following message: {$message}"; $response = "Dear {$firstname}, Thank you for contacing mycompany, we have received your message and will respond within 24 hours. Sincerely, The Team"; mail($Addreses[$value], 'Message sent to mycompany', $emailmessage, "FROM:$email"); } } if ($response) mail($email, 'Your message to Mycompany',$response, "FROM:noreply@mycompany.com\r\n"); } ?> (neither tested so there might be a typo in them). All the best Keith Quote Link to comment 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.