How to send select values to email?

I have a contact page and I would like to add certain options when filling out the form. Like this
<select>
<option value=“media”>select option</option>
<option value=“music”>option 1</option>
<option value=“movie”>option 2</option>
</select>
But I don’t know how to send the option that the user has selected to my email. What do i need to do to my php file to send the value they selected? thanks.
Also, how do make it mandatory for the user to select an option?

<?php

$myemail = “”;

$email = check_input($_POST[‘email’]);
$message = check_input($_POST[‘message’], “Write your message”);

if (!preg_match(“/([\w\-]+\@[\w\-]+\.[\w\-]+)/”, $email))
{
show_error(“E-mail address not valid”);
}

$message = "

E-mail: $email

Message:
$message

";

mail($myemail, $subject, $message);

header(‘Location: thanks.html’);
exit();

function check_input($data, $problem=‘’)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error: </p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

Try <select name=“type”>
Then…


$type = check_input($_POST['type']);

$message = "

E-mail: $email

Type: $type

Message:
$message

With regards to making selecting an option mandatory, I guess you could set your first option to trigger an error.
<option value=“error”>Please select an option</option>

Then test if the option value is “error” and echo an error.


if ($_POST['type'] = 'error'){

  //whatever you want to do to echo out the error message

}

Thanks. :slight_smile:

I tried this…

<?php
$myemail = “”;

$email = check_input($_POST[‘email’]);
$message = check_input($_POST[‘message’], “Write your message”);
$type = check_input($_POST[‘type’]);

if (!preg_match(“/([\w\-]+\@[\w\-]+\.[\w\-]+)/”, $email))
{
show_error(“E-mail address not valid”);
}
if ($_POST[‘type’] = ‘error’)
{
show_error(“Please select a type”);
}

$message = "

E-mail: $email

Type: $type

Message:
$message

";

mail($myemail, $subject, $message);

header(‘Location: thanks.html’);
exit();

function check_input($data, $problem=‘’)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p><h3>Please correct the following error:</h3></p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

But for some reason, no matter what I select it brings me to this page.

function show_error($myError)
{
?>
<html>
<body>

<p><h3>Please correct the following error:</h3></p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>

Even if I select option 1 or 2 which do not have the value “error”. Any idea why?

<select name=“type”>
<option value=“error”>select option</option>
<option value=“music”>option 1</option>
<option value=“movie”>option 2</option>
</select>