michytttt Posted June 1, 2011 Share Posted June 1, 2011 Hi Everyone, Very new to PHP and databases, so please go easy on me! I have a series on web pages each containing a link that says 'apply here'. What I am trying to achieve is when any of these links are clicked on the user is taken to a php form page where they are asked for a series of further information such as name, telephone number etc. When they click submit this information then gets sent off to a speciifc email address. My problem is that depending on which page the user has originally come from (the page where they clicked on the apply here link) needs to determine what email address the form information gets sent to. I thought that the best way to achieve this would be to create a database that contains all of the email addresses (as eventually there will be alot of them) and the form page could call in the relevant email somehow... although I am totally confused on how to go about doing this. Any help would be great! Thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/ Share on other sites More sharing options...
TOA Posted June 1, 2011 Share Posted June 1, 2011 That would work. Your link would send a unique ID to the php form which goes to get the email associated with that ID from the db. Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223685 Share on other sites More sharing options...
michytttt Posted June 1, 2011 Author Share Posted June 1, 2011 Great news, How would I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223688 Share on other sites More sharing options...
TOA Posted June 1, 2011 Share Posted June 1, 2011 Your table would have fields ID and Email, and whatever else you want. Each of your links would include a variable in the url -- the unique id from the db table. Then in your php script, grab the number sent $id = $_GET['id'] Then query the db for that email SELECT Email FROM Your_Table WHERE ID='$id' Then use that returned value in the mail function Hope that makes sense Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223694 Share on other sites More sharing options...
michytttt Posted June 1, 2011 Author Share Posted June 1, 2011 Yes it does make some sense to me so the link would be something like www.mydomainname.co.uk/form.php?id=222 the $id = $_GET['id'] will grab this unique id then the SELECT Email FROM Your_Table WHERE ID='$id' will find the relevant email I think I get this thanks. In the actual form itself how would I tell it to send it to the relevant email address? Is it in the mail($to,$subject,$message,$headers); part? Thanks for all your advice! Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223700 Share on other sites More sharing options...
TOA Posted June 1, 2011 Share Posted June 1, 2011 Yes it does make some sense to me so the link would be something like www.mydomainname.co.uk/form.php?id=222 the $id = $_GET['id'] will grab this unique id then the SELECT Email FROM Your_Table WHERE ID='$id' will find the relevant email Yes, exactly In the actual form itself how would I tell it to send it to the relevant email address? Is it in the mail($to,$subject,$message,$headers); part? Yes, $to would be the returned value from the db Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223712 Share on other sites More sharing options...
michytttt Posted June 1, 2011 Author Share Posted June 1, 2011 Sorry to be a pain but how would I tell it that the $to is to collect the correct email? Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223717 Share on other sites More sharing options...
TOA Posted June 1, 2011 Share Posted June 1, 2011 There's other ways, but here's one: $sql = "SELECT Email FROM your_table WHERE ID='$id'"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { $to = $row['Email']; } Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223722 Share on other sites More sharing options...
michytttt Posted June 1, 2011 Author Share Posted June 1, 2011 You have been really helpful, I have got the following so far but I cannot seem to get it to work: <?php $username = "username"; $password = "password"; $hostname = "localhost"; $id = $_GET['id']; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Connected to MySQL<br>"; $selected = mysql_select_db("db",$dbhandle) or die("Could not select db"); $sql = "SELECT email FROM emails WHERE ID='$id'"; $result = mysql_query($sql);while ($row = mysql_fetch_assoc($result)) { $to = $row['email'];} ?> The form begins: <?php function spamcheck($field) { $field=filter_var($field, FILTER_SANITIZE_EMAIL); if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } } function validate_form() { $errors = array(); if(!(isset($_POST['name']) && (strlen($_POST['name']) > 3))){ $errors['name'] = '<span style="color:red; font-size:12px">*Please enter a contact name</span>'; } if(!(isset($_POST['address']) && (strlen($_POST['address']) > 3))){ $errors['address'] = '<span style="color:red; font-size:12px">*Please enter a valid e-mail address</span>'; } $mailcheck = spamcheck($_POST['address']); if ($mailcheck==FALSE) { $errors['address'] = '<span style="color:red; font-size:12px">*Please enter a valid e-mail address</span>'; } if(!(isset($_POST['telephone']) && (strlen($_POST['telephone']) > 3))){ $errors['telephone'] = '<span style="color:red; font-size:12px">*Please enter a contact No</span>'; } $phone = ereg_replace('[^0-9]', '', $_POST['telephone']); if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) { $errors['telephone'] = '<span style="color:red; font-size:12px">*Please enter a contact No</span>'; } if(!(isset($_POST['message']) && (strlen($_POST['message']) > 3))){ $errors['message'] = '<span style="color:red; font-size:12px">*Please include your message</span>'; } return $errors; } function display_form($errors) { $defaults['name']= isset($_POST['name']) ? htmlentities($_POST['name']) : ''; $defaults['address']= isset($_POST['address']) ? htmlentities($_POST['address']) : ''; $defaults['telephone']= isset($_POST['telephone']) ? htmlentities($_POST['telephone']) : ''; $defaults['message']= isset($_POST['message']) ? htmlentities($_POST['message']) : ''; ?> <!--name of page with form on--> <form action='apply-for-job5.php' method='post'> <table border="0" cellpadding="2" cellspacing="0" class="tablecolour" > <tr> <td width="38%" valign="top">Name*</td> <td width="62%"> <input type='text' name='name' value='<?php echo $defaults['name']?>'/> <?php print_error('name',$errors)?><br/></td> </tr> <tr> <td valign="top"><label for="tswemail"> Email Address* </label></td><td><input type='text' name='address' value='<?php echo $defaults['address']?>'/> <?php print_error('address',$errors)?><br/></td> </tr> <tr> <td valign="top"><label for="tswphone"> Contact Number* </label></td><td> <input type='text' name='telephone' value='<?php echo $defaults['telephone']?>'/> <?php print_error('telephone',$errors)?><br/></td> </tr> <tr> <td valign="top"><label for="tswcomments"> Your Message*</label><br /></td> <td valign="top"> <textarea rows="5" cols="47" name="message"> <?php echo $defaults['message']?></textarea> <?php print_error('message',$errors)?><br/></td> </tr> <tr> <td height="32" valign="top">*Required Fields </td> <!--change button name here--> <td valign="top" align="right"><input type="submit" value="Send" /></td> </tr> </table> </form> <?php } function print_error($key, $errors){ if(isset($errors[$key])){ print "<dd class='error'>{$errors[$key]}</dd>"; } } if($_SERVER['REQUEST_METHOD']== 'POST'){ $errors = validate_form(); if(count($errors)){ display_form($errors); }else{ echo '<h1 class="h1blue">Thankyou...'.ucwords($_POST['name']).' A member of our team will contact you shortly</h1>'; //===========Email address her=========// $subject = "Email Request"; $message = 'Contact Name: '.$_POST['name']; $message.= "\n".' Message: '.$_POST['message']; $message.= "\n".' Telephone no: '.$_POST['telephone']; $from = $_POST['address']; $headers = "From: $from"; mail($to,$subject,$message,$headers); } }else { display_form($errors); } ?> Hope this helps! MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223731 Share on other sites More sharing options...
Pikachu2000 Posted June 1, 2011 Share Posted June 1, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Also, this has little, if anything to do with MySQL, so moving thread to PHP help . . . Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223732 Share on other sites More sharing options...
TOA Posted June 1, 2011 Share Posted June 1, 2011 You have been really helpful, I have got the following so far but I cannot seem to get it to work: Not enough info How does it not work? Do you have error reporting on? Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1223760 Share on other sites More sharing options...
michytttt Posted June 2, 2011 Author Share Posted June 2, 2011 Sorry for late reply. The form seems to work but I do not receive the email to the required address. Quote Link to comment https://forums.phpfreaks.com/topic/238136-sending-to-different-email-addresses-from-a-database/#findComment-1224240 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.