Jump to content

Php Contact Form With Drop-down!


driz
 Share

Recommended Posts

Hi, I'm creating a simple PHP contact form, that will have a drop-down menu for the recipients!This is my code:

<form method="post" action=""><ul id="contact_form">	<li>		<label for="dept">To</label>		<select name="dept">			<option value="" disabled="disabled" selected="selected">Please choose one...</option>			<optgroup label="Support">				<option value="dave@dave.co.uk">Dave</option>				<option value="andrew@andrew.co.uk">Andrew</option>				<option value="chris@chris.co.uk">Chris</option>			</optgroup>		</select>	</li>	<li>		<label for="name">Name</label>		<input type="text" id="name" name="name" value="" maxlength="100" />	</li>	<li>		<label for="company">Company</label>		<input type="text" id="company" name="company" value="" maxlength="100" />	</li>		<li>		<label for="tel">Telephone</label>		<input type="text" id="tel" name="tel" value="" maxlength="100" />	</li>	<li>		<label for="email">Email address</label>		<input type="text" id="email" name="email" value="" maxlength="100" />	</li>	<li>		<label for="message">Inquiry</label>		<textarea id="message" name="message"></textarea>	</li>	<li>		<label></label>		<input type="submit" value="Send" />	</li></ul></form>

I have thought about using PHP mail function, but was wondering if someone could post a better way of doing it? SAMPLES WOULD BE AWESOME! All I need is for the content to be mailed to the email address selected from the drop-down, also im not sure if the email address should be in the values or in the php code? thanks.

Link to comment
Share on other sites

The mail function is the common way of sending mail. If you don't want to use that, the PEAR package has a mail class also. If the email address gets submitted to the page it will be able to be picked up by spambots looking for addresses to spam, and a spammer could also use any other address they want to send the mail wherever they want it to go. It's usually better to put the addresses in the PHP code.http://www.php.net/manual/en/function.mail.phphttp://pear.php.net/package/Mail

Edited by justsomeguy
Link to comment
Share on other sites

Hi, I'm creating a simple PHP contact form, that will have a drop-down menu for the recipients!This is my code:
<?php$dropdown = isset($_POST['test']) ? $_POST['test'] : '';$look = ''; //just a custom echo$ssr = $_POST['dept']; //get the data form the selectif($dropdown == "1"){    if($_POST['done']){        if ($ssr == 'Dave') {             //put what you want here if they pick Dave        }        elseif ($ssr == 'Andrew'){             //put what you want here if they pick Andrew        }        elseif ($ssr == 'Chris'){            //put what you want here if they pick Chris        }        else {            //put somthing here if they dont select anything        }    }}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <title></title>    </head>    <body><form method="post"><input type="hidden" name="test" value="1" /><?php echo $look ?><ul id="contact_form">    <li>        <label for="dept">To</label>        <select name="dept">            <option value="" disabled="disabled" selected="selected">Please choose one...</option>            <optgroup label="Support">                <option>Dave</option>                <option>Andrew</option>                <option>Chris</option>            </optgroup>        </select>    </li>    <li>        <label for="name">Name</label>        <input type="text" id="name" name="name" value="" maxlength="100" />    </li>    <li>        <label for="company">Company</label>        <input type="text" id="company" name="company" value="" maxlength="100" />    </li>        <li>        <label for="tel">Telephone</label>        <input type="text" id="tel" name="tel" value="" maxlength="100" />    </li>    <li>        <label for="email">Email address</label>        <input type="text" id="email" name="email" value="" maxlength="100" />    </li>    <li>        <label for="message">Inquiry</label>        <textarea id="message" name="message"></textarea>    </li>    <li>        <label></label>        <input type="submit" value="Send" name="done" />    </li></ul></form>    </body></html>

i'll post the code for if you want to use anther page for the php coding.and like he said make a code for spam$ssr don't mean anything its just what i put for stuff like this

Edited by TKW22
Link to comment
Share on other sites

Thanks for your replies. I think the best option will be to create a simple ONE PAGE .php file that will house both the form and the php process code, I'm looking for the simplest way of building a drop-down menu to choose the email address, and im not bothered about spam and the like (its a prototype) Also i dont want the code to process until the user has hit the submit button. I'm not sure how to do this, I remember doing something similar a while back with a variable and if statement, but it seemed buggy and incorrect, thanks.

Edited by driz
Link to comment
Share on other sites

I'm really struggling with this, could someone post an example of using the MAIL function and the drop down.So far I have this code:

<?phpif(isset($_POST['submit'])) {$to = "";$subject = "";$name = $_POST['name'];$company = $_POST['company'];$tel = $_POST['tel'];$email = $_POST['email']; $body = "From: $name\n Company: $company\n Tel: $tel\n E-Mail: $email\n Message:\n $message"; echo "Data has been submitted to $to!";mail($to, $subject, $body);} else {echo "blarg!";}?>

So now all I need is to be able to grab the data from a drop right? How do I do this, I'm going to go with putting the actual values in the drop-down box.like so:

				<option value="dave@dave.co.uk">Dave</option>				<option value="andrew@andrew.co.uk">Andrew</option>				<option value="chris@chris.co.uk">Chris</option>

Edited by driz
Link to comment
Share on other sites

PHP

 <?php # Script 10.1 - contact.php// Check for form submission:if (isset($_POST['contact_submit'])){	//Form validation	if(!empty($_POST['contact']) &&			!empty($_POST['contact_name']) &&		!empty($_POST['contact_email']) &&		!empty($_POST['comments']) )		{			//Create the body:					$body = "Name:			{$_POST['contact_name']}\n\n					 Email:			{$_POST['contact_email']}\n\n					 Comments:			{$_POST['comments']}";					//Make it no longer than 100 character:					$body = wordwrap($body, 100);					//Send the email:					mail("{$_POST['contact']}",			'A Message from the Website', $body,			"From: {$_POST['contact_email']}");						//Print a message					echo '<p><em>Thank you, your request has been processed.</em></p>';					//Clear $_POST (so that the form's not sticky					$_POST = array();		}				else		{			echo '<p style="font-weight: bold;			color: #C00">Its kind a hard to sing to your friends if you cant see them...Try again please.</p>';		}	} //end of main isset() If//Create the HTML form?>

HTML

<form action="index.php" method="post">      <p>Contacts: <br/>        <select name="contact">          <option value="owen@analogstudios.net">Owen (Analog Studios)</option>          <option value="thehappymexican@yahoo.com">Aldon (Manager -           Electro Calrissian)</option>		  <option value="roryboyan@yahoo.com">Rory Boyan</option>          <?php if (isset($_POST['contact'])) echo 	$_POST['contact']; ?>        </select>      </p>      <p>Your Name:         <input type="text" name="contact_name" 	size="30" maxlength="80" value="<?php if	(isset($_POST['contact_name'])) echo $_POST['contact_name']; ?>" />      </p>      <p>Your Email:  <br/>        <input type="text" name="contact_email" 	size="30" maxlength="80" value="<?php if	(isset($_POST['contact_email'])) echo $_POST['contact_email']; ?>" />      </p>      <p>Comments:<br/>        <textarea name="comments" rows="5" cols="29">		<?php if (isset($_POST['comments'])) echo 	$_POST['comments']; ?></textarea>      </p>      <input type="submit" name="submit" value="Contact Us"/>      <input type="hidden" name="contact_submit" value="email_contact"/>    </form>

this code makes the form "sticky" so it won't change pages after you hit submit.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...