Jump to content

Sending to different email addresses from a database


michytttt

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

 

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?

 

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.