我是PHP的新手,我正在尝试使用<select>
和<option>
下拉列表来处理我的联系表单。
联系表单将所有信息发送到电子邮件,<select>
和<option>
字段除外
这是我的代码:
PHP代码:
<?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\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n";
// 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("\r\n", $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代码:
<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>
谁能看到我在这里做错了什么?感谢任何帮助,谢谢!
答案 0 :(得分:3)
请将名称添加到下拉菜单中,而不是获取值。
您希望服务器端脚本的所有元素都必须包含 name 属性。
代码。
<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>
由于 阿米特
答案 1 :(得分:1)
您的选择没有名称属性