我是PHP的新手,我真的不知道我做错了什么,或者即使我做了什么是正确的,但它似乎有效,但只有下拉菜单不能完全正常工作。
$ SelectThree和$ SelectFour仅显示最后一个选择...例如,如果您选择DropDown选项A和B ...只有B将显示在电子邮件中...即:
Name: name
Number: number
Date: 10/06/2014
Select: DropdownA
Select2: DropdownA
Select3: Drop Down Option B
Select4: Drop Down Option B
Radio: female
Checkbox: OptionB
Switch: On
Email: example@example.com
Message: message
PHP:
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
ob_start();
if(isset(
$_REQUEST['name'],
$_REQUEST['email'],
$_REQUEST['message'],
$_REQUEST['number'],
$_REQUEST['date'],
$_REQUEST['select'],
$_REQUEST['selectTwo'],
$_REQUEST['selectThree'],
$_REQUEST['selectFour'],
$_REQUEST['radio'],
$_REQUEST['checkbox'],
$_REQUEST['switch'],
$_REQUEST['token'] )){
if($_SESSION['token'] != $_POST['token']){ $response = "0";
} else {
$_SESSION['token'] = "";
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$number = $_REQUEST['number'];
$date = $_REQUEST['date'];
$select = $_REQUEST['select'];
$selectTwo = $_REQUEST['selectTwo'];
$selectThree = $_REQUEST['selectThree'];
$selectFour = $_REQUEST['selectFour'];
$radio = $_REQUEST['radio'];
$checkbox = $_REQUEST['checkbox'];
$switch = $_REQUEST['switch'];
switch (true){
case !filter_var($email, FILTER_VALIDATE_EMAIL):
$response = "<p style='color:red'>Invalid Email Address!</p>";
break;
default:
$to = "support@loaidesign.co.uk";
$subject = "New Message From: $name";
$message = "Name: $name <br/>
Number: $number <br/>
Date: $date <br/>
Select: $select <br/>
Select2: $selectTwo <br/>
Select3: $selectThree <br/>
Select4: $selectFour <br/>
Radio: $radio <br/>
Checkbox: $checkbox <br/>
Switch: $switch <br/>
Email: $email <br/>
Message: $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: '."support@loaidesign.co.uk" . "\r\n";
$headers .= 'Reply-To: '.$email . "\r\n";
$params = '-f support@loaidesign.co.uk';
$mailed = (mail($to, $subject, $message, $headers));
if( isset($_REQUEST['ajax'])){ $response = ($mailed) ? "1" : "0";
} else {
$response = ($mailed) ? "<h2>Success!</h2>" : "<h2>Error! There was a problem with sending.</h2>";
}
break;
}
echo $response;
}
} else {
echo "Error";
}
ob_flush();
die();
}
?>
联系表单HTML:
<!--Contact Form-->
<?php $token = md5(uniqid(rand(), TRUE)); $_SESSION['token'] = $token;?>
<form id="contactForm" class="validate" name="contactForm" action="contact.php" method="post">
<input name="token" type="hidden" value="<?php echo $token; ?>">
<input name="ajax" type="hidden" value="1">
<fieldset>
<p>Your Name</p>
<input name="name" class="name required fullname" autocomplete="off">
</fieldset>
<fieldset>
<p>Email Address</p>
<input name="email" type="email" class="email required" autocomplete="off">
</fieldset>
<fieldset>
<p>Message</p>
<textarea name="message" rows="5" class="required min3"></textarea>
</fieldset>
<fieldset>
<p>Phone Number</p>
<input name="number" class="hasIcon" autocomplete="off">
<i class="form-icon icon-phone"></i>
</fieldset>
<fieldset>
<p>Time</p>
<input name="time" class="mask-time hasIcon" autocomplete="off">
<i class="form-icon icon-clock"></i>
</fieldset>
<fieldset>
<p>Date</p>
<input name="date" class="required date calendar hasIcon" autocomplete="off">
<i class="form-icon icon-calendar"></i>
</fieldset>
<fieldset>
<p class="inline">Date Selected:</p>
<input class="selectedDate inline"></input>
</fieldset>
<fieldset>
<p>Dropdown Menu</p>
<select name="select" class="select required" data-placeholder="Choose an option">
<option value=""></option>
<option value="DropdownA">DropdownA</option>
<option value="DropdownB">DropdownB</option>
</select>
</fieldset>
<fieldset>
<p>Dropdown Menu - Searchable</p>
<select name="selectTwo" class="select-search" data-placeholder="Choose an option">
<option value=""></option>
<option value="DropdownA">DropdownA</option>
<option value="DropdownB">DropdownB</option>
</select>
</fieldset>
<fieldset>
<p>Multi Options Dropdown Menu</p>
<select name="selectThree" class="select multi-select" multiple="multiple" tabindex="-1" data-placeholder="Choose an option">
<option value=""></option>
<optgroup label="Section One">
<option>Drop Down Option A</option>
<option>Drop Down Option B</option>
</optgroup>
<optgroup label="Section Two">
<option>Drop Down Option A</option>
<option>Drop Down Option B</option>
<option>Drop Down Option C</option>
<option>Drop Down Option D</option>
</optgroup>
</select>
</fieldset>
<fieldset>
<p>Multi Options Dropdown Menu - Min 2 & Max 3</p>
<select name="selectFour" class="select multi-select" multiple="multiple" tabindex="-1" min="2" max="3">
<option value=""></option>
<option>Drop Down Option A</option>
<option>Drop Down Option B</option>
<option>Drop Down Option A</option>
<option>Drop Down Option B</option>
</select>
</fieldset>
<fieldset class="checkbox">
<p>Checkboxs:</p>
<label><input name="checkbox" type="checkbox" value="OptionA" class="required"><span class="checked-icon"><span></span></span><span>Option A</span></label>
<label><input name="checkbox" type="checkbox" value="OptionB" class="required"><span class="checked-icon"><span></span></span><span>Option B</span></label>
</fieldset>
<fieldset class="radio">
<p>Radios:</p>
<label><input name="radio" type="radio" value="male" class="required"><span class="checked-icon"><span></span></span><span>Male</span></label>
<label><input name="radio" type="radio" value="female" class="required"><span class="checked-icon"><span></span></span><span>Female</span></label>
</fieldset>
<fieldset class="switch">
<p>Switch:</p>
<label><input name="switch" type="checkbox" value="On"><span><span></span></span></label>
</fieldset>
<button id="submit" type="submit">Send</button>
</form>
答案 0 :(得分:3)
要使其有效,您应该在HTML中指定name="selectFour"
而不是name="selectFour[]"
编辑:PHP方面,您可以利用$ _REQUEST [&#39; selectFour&#39;]作为数组。
编辑:也许,您可以使用内爆函数http://php.net/manual/en/function.implode.php