简体   繁体   中英

Post select option PHP

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>

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>

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM