繁体   English   中英

发布选择选项PHP

[英]Post select option PHP

我不知道如何开机自检选择选项。 表单为联系人(选项)字段发布空值。 我知道selected_val不正确,但不确定如何更正。

表单过得很好,但是contact返回一个空值。 任何人都可以帮忙解决如何纠正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>

值属性缺失应该是这样的

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

您需要选项标签的value

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

而且我没有看到任何输入名称submit您的HTML

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

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

同样,您的PHP代码这一行什么都不做。

"You have selected :" .$selected_val;

应该

print "You have selected :" .$selected_val;

要么

echo "You have selected :" .$selected_val;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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