Jump to content

Recommended Posts

I'm looking to build a contact form with a drop-down menu for different recipients of the form.

Does anyone know of a good tutorial for how to integrate PHP with drop-down menus or a site that explains this type of situation?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/39815-solved-contact-form-with-drop-down-menu/
Share on other sites

Well any general form tutorial will do what you need. In this case, you would build a completely normal drop down box in the HTML, then on the PHP end you would have it check what the user selected, then send.

 

Take a look at this:

 

http://www.tizag.com/phpT/examples/formex.php

You don't want to do anything like this:

form_handler.php

<?php
  $sel = $_POST['selected'];
  if($sel == "Bob"){
    // Do bob stuff
  }else if($sel == "Sally"){
    // Do sally stuff
  }else if($sel == "Fred"){
    // Do fred stuff
  }
  // ...and so on.
?>

 

It's very inefficient, tedious to type, and impossible to maintain.

 

Instead set the value attribute of the form options to an appropriate value, such as the e-mail address of the recipient.

form

<select name="recipient">
  <option value="bob@co.com">Bob</option>
  <option value="sally@co.com">Sally</option>
  <option value="fred@co.com">Fred</option>
</select>

 

form_handler.php

<?php
  $email = $_POST['recipient'];
  mail($email, "Hi2u", "Hello, World!");
?>

try this ok .

 

<?php
$db=mysql_connect("localhost","username","password");
mysql_select_db("database_name",$db);


$query="select * form what_ever where email='$email'";

$result=mysql_fetch_assoc($query);

echo"<form='method='POST' action=''> <option name='email'>";

while($x=mysql_fetch_assoc($result)){

echo"<option value='".$x['email']."'>".$x['email']."</option>";
}

echo"<input type='submit' name='submit' value='email members'></select>

</form></option>";


if($_POST['submit']){

$to = $email;

$subject = 'hi it redarrow!';

$message = '<b>yo</b>, how are you mate';

$headers = "From: redarrow@what_ever.com\r\n" .
       'X-Mailer: PHP/' . phpversion() . "\r\n" .
       "MIME-Version: 1.0\r\n" .
       "Content-Type: text/html; charset=utf-8\r\n" .
       "Content-Transfer-Encoding: 8bit\r\n\r\n";

if(mail($to, $subject, $message, $headers)){

echo " cheers email sent to: $email ";
}

}else{

echo" sorry no email sent";
}

?>

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.