簡體   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