1

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>
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
ControlZ
  • 185
  • 2
  • 16

2 Answers2

3

value attribute missing It should be like this

<select class="form-control" id="contact" name="contact">
 <option>select</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • Yup sure it is - duh! So after fixing this the option value is still posting nothing. I think this is incorrect: $selected_val = $_POST['contact']; "You have selected :" .$selected_val; – ControlZ Jan 06 '17 at 04:04
  • 1
    you have to select anyone either email or phone then you have to submit the form because first optin is empty . – JYoThI Jan 06 '17 at 04:06
  • echo missing $selected_val = $_POST['contact']; echo "You have selected :" .$selected_val; – JYoThI Jan 06 '17 at 04:07
  • I get ya, but that's not working either. Reload page and select option second option (phone) but it still posts nothing. – ControlZ Jan 06 '17 at 04:11
  • print_r($_POST); exit; on destination page and know the all post values – JYoThI Jan 06 '17 at 04:15
  • Figured it out -> $selectOption = $_POST['contact']; – ControlZ Jan 06 '17 at 04:41
1

You need value on option tags.

<option value="email">Email</option>
<option value="phone">Phone</option>

And I don't see any input name submit in your HTML

if( isset($_POST["submit"]) ) {

If so, it's not even go in to your code.

Also this line of your PHP code is doing nothing.

"You have selected :" .$selected_val;

should be

print "You have selected :" .$selected_val;

or

echo "You have selected :" .$selected_val;
Hereblur
  • 2,084
  • 1
  • 19
  • 22