发布选择选项PHP

时间:2017-01-06 03:52:27

标签: php twitter-bootstrap formmail

我无法弄清楚如何POST选择选项。表单发布联系人(选项)字段的空值。我知道selected_val不正确,但不知道如何纠正。

表单发布正常,但联系人返回一个空值。任何人都可以帮忙解决如何更正PHP。

这是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>

2 个答案:

答案 0 :(得分:3)

值属性缺失它应该是这样的

<select class="form-control" id="contact" name="contact">
 <option>select</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>

答案 1 :(得分:1)

您需要value选项标记。

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

我在HTML

中看不到任何输入名称submit
if( isset($_POST["submit"]) ) {

如果是这样,它甚至不会进入您的代码。

此代码的PHP代码也无效。

"You have selected :" .$selected_val;

应该是

print "You have selected :" .$selected_val;

echo "You have selected :" .$selected_val;
相关问题