dousikuai5417 2016-01-07 02:41 采纳率: 0%
浏览 68
已采纳

联系表单,选择选项字段不起作用

I'm new to PHP and i'm trying to make my contact form work with <select> and <option> dropdown.

The contact form sends all information to the e-mail, except for the <select> and <option> fields

Here is the code I have:

PHP Code:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $number = $_POST['number'];
        $from = 'Ny melding sendt fra kontaktskjema'; 
        $to = 'test@mail.com'; 
        $subject = 'CompanyName | Dere har motatt en ny melding';
        $headers   = array();
        $headers[] = "MIME-Version: 1.0";
        $headers[] = "Content-type: text/plain; charset=UTF-8";
        $headers[] = "From: Ny melding sendt fra kontaktskjema på FantasyLab.no";
        $headers[] = "X-Mailer: PHP/".phpversion();
        $headers[] = "Reply-To: $name <$email>";

        $body       = "Fra: $name
 E-post: $email
 Telefonnummer: $number
 Melding: $message
";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Vennligst skriv inn ditt navn';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Vennligst skriv inn din e-post';
        }

        //Check if message has been entered
        if (!$_POST['number']) {
            $errNumber = 'Vennligst skriv inn ditt telefonnummer';
        }

        //Check if message has been entered
        if (!$_POST['budget']) {
            $errBudget = 'Vennligst skriv en melding';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Vennligst skriv en melding';
        }

         (isset($_POST["budget"])) ? $budget = $_POST["budget"] : $budget=1;

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage && !$errBudget) {
    if (mail ($to, $subject, $body,implode("
", $headers))) {
        $result='Thank you for your enquiry. We will get in touch within 24 hours.';
        header('Location:index.php?success='.$result);
    } else {
        $result='We apologize, something went wrong. Let us talk over phone; +47 45 46 46 07';
        header('Location:index.php?fail='.$result);
    }
}
    }
?>

HTML Code:

    <div class="form-group col-md-6 col-sm-12 col-xs-12">
            <div class="col-xs-12">
            <div class="option-styling">
            <label>Budget*</label>                                  
            <select id="budget"class="col-xs-12">
              <option <?php if ($budget == 1 ) echo 'selected' ; ?> value="1">10.000 kr - 25.000 kr</option>
              <option <?php if ($budget == 2 ) echo 'selected' ; ?> value="2">25.000 kr - 50.000 kr</option>
              <option <?php if ($budget == 3 ) echo 'selected' ; ?> value="3">50.000 kr +</option>
              <option <?php if ($budget == 4 ) echo 'selected' ; ?> value="4">Not sure</option>
            </select>
            <?php echo "<p class='text-danger'>$errBudget</p>";?>

        </div>     
            </div>
    </div>

Can anyone see what I am doing wrong here? Any help is appreciated, thanks!

  • 写回答

2条回答 默认 最新

  • duanqian9503 2016-01-07 02:45
    关注

    Please add the name to dropdown than you will get the values.

    All elements which you want to server side script must have name attribute in it.

    in your code.

    <div class="form-group col-md-6 col-sm-12 col-xs-12">
                <div class="col-xs-12">
                <div class="option-styling">
                <label>Budget*</label>                                  
                <select name="budget" id="budget"class="col-xs-12">
                  <option <?php if ($budget == 1 ) echo 'selected' ; ?> value="1">10.000 kr - 25.000 kr</option>
                  <option <?php if ($budget == 2 ) echo 'selected' ; ?> value="2">25.000 kr - 50.000 kr</option>
                  <option <?php if ($budget == 3 ) echo 'selected' ; ?> value="3">50.000 kr +</option>
                  <option <?php if ($budget == 4 ) echo 'selected' ; ?> value="4">Not sure</option>
                </select>
                <?php echo "<p class='text-danger'>$errBudget</p>";?>
    
            </div>     
                </div>
        </div>
    

    Thanks Amit

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题