I cannot figure out how to POST select options. The form posts empty values for the contact (option) field. I know the selected_val is incorrect but not sure how to correct.
The form posts fine, but contact returns an empty value. Can anyone help out with how to correct the PHP.
Here's the PHP
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$selected_val = $_POST['contact']; "You have selected :" .$selected_val;
$message = $_POST['message'];
$from = 'From your website';
$to = 'somedomain.com';
$subject = 'Visitor Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\nCity: $city\nContact: $contact\nMessage: $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if business has been entered
if (!$_POST['company']) {
$errBusiness = 'Please enter your business or organization name';
}
// Check if phone has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone number';
}
// Check if phone has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter the name of your city';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You!
We will be in touch soon.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
HTML
<form class="form-horizontal form-elements" role="form" method="post" action="services-form.php">
<div class="col-md-8 col-md-offset-2">
<label for="contact">Contact preference</label>
<select class="form-control" id="contact" name="contact">
<option>Email</option>
<option>Phone</option>
</select>
</div>
</form>
You need
value
on option tags.And I don't see any input name
submit
in your HTMLIf so, it's not even go in to your code.
Also this line of your PHP code is doing nothing.
should be
or