dousikuai5417
dousikuai5417
2016-01-07 02:41

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

  • php
  • html
  • html-select
已采纳

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 duanqian9503 5年前

    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

    点赞 评论 复制链接分享
  • dongyan5815 dongyan5815 5年前

    Your select has no name attribute

    点赞 评论 复制链接分享