我已经让这个表格完全符合我的需要,除了在颜色部分,我在表单提交时收到的电子邮件中没有显示所选内容。它只是空白。任何帮助都会很棒...... 我在js行上尝试了很多不同的变体:var color = $(“#color option:selected”)。text();从这里阅读其他问题,我也尝试过:
$color = $_POST['color'][0];
$color = $_POST['color'][1];
$color = $_POST['color'][2];
$color = $_POST['color'][3];
$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
var email = $("#email").val();
var color = $("#color option:selected").text();
var favorite_color = $("#favorite_color").val();
var friend_referral = ($('#friend_referral').is(':checked')) ? $('#friend_referral').val() : false;
var search_engine = ($('#search_engine').is(':checked')) ? $('#search_engine').val() : false;
var biz_card = ($('#biz_card').is(':checked')) ? $('#biz_card').val() : false;
var website = ($('#website').is(':checked')) ? $('#website').val() : false;
$("#returnmessage").empty(); // To empty previous error/success message.
// Checking for blank fields.
if (name === '' || email === '') {
alert("Please Fill Required Fields");
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("test_form.php", {
name: name,
email: email,
color: color,
favorite_color: favorite_color,
friend_referral: friend_referral,
search_engine: search_engine,
biz_card: biz_card,
website: website
}, function(data) {
$("#returnmessage").append(data); // Append returned message to message paragraph.
if (data == "Your form has been received, We will contact you soon.") {
$("#catalog_form")[0].reset(); // To reset form fields on success.
}
});
}
});
}); < /script>
<form id="catalog_form">
<label for="name">Name: <span>*</span>
</label>
<br>
<input type="text" id="name" />
<br>
<label for="email">Email: <span>*</span>
</label>
<br>
<input type="email" id="email" />
<fieldset>
<legend>Color</legend>
<select name="color">
<option value="">Select One</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
<option value="green">Green</option>
</select>
<label>My favorite Color is:</label>
<input type="text" id="favorite_color" />
</fieldset>
<fieldset>
<legend>How Did You Find Our Website?</legend>
<input type="checkbox" value="true" id="friend_referral" />
<label for="friend_referral">Friend Referral</label>
<br>
<input type="checkbox" value="true" id="search_engine" />
<label for="search_engine">Search Engine (like Google or Yahoo!)</label>
<br>
<input type="checkbox" value="true" id="biz_card" />
<label for="biz_card">Business Card</label>
<br>
<input type="checkbox" value="true" id="website" />
<label for="website">Web Site Link</label>
</fieldset>
<input type="button" id="submit" value="Send" />
<br>
<p id="returnmessage"></p>
</form>
<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$color = $_POST['color'];
$favorite_color = $_POST['favorite_color'];
$friend_referral = $_POST['friend_referral'];
$search_engine = $_POST['search_engine'];
$biz_card = $_POST['biz_card'];
$website = $_POST['website'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>Thank you for contacting us!<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Color:' . $color . '<br/>'
. 'favorite_color:' . $favorite_color . '<br/>'
. 'friend_referral:' . $friend_referral . '<br/>'
. 'search_engine:' . $search_engine . '<br/>'
. 'biz_card:' . $biz_card . '<br/>'
. 'website:' . $website . '<br/><br>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We will contact you as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#333; color:white;\">" . $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("justin@rabbiddesign.com", $subject, $sendmessage, $headers);
echo "Your form has been received, We will contact you soon.";
} else {
echo "<span>* invalid email *</span>";
}
?>
答案 0 :(得分:1)
您正在使用选择器:
var color = $("#color option:selected").text();
通过ID选择,但select没有此id,只需添加ID“color”即可解决颜色问题:
<select name="color" id="color">
更新至原始回答:
我还注意到您正在使用.text()
,这意味着当您没有选择颜色时,您将通过Select one
而不是""
。将最终选择器更改为:
var color = $("#color option:selected").val();
我相信也可以简单地致电:
var color = $("#color").val();
但我不喜欢这个选项,因为我喜欢区分文本输入.val()和select .val() - 个人偏好,你可以做你喜欢的事情
答案 1 :(得分:0)
您必须按照您使用的ID引用select标记,即#color id
,因此您的代码可能受以下因素的影响:
选择标记的代码:
<fieldset>
<legend>Color</legend>
<select name="color" id="color">
<option value="">Select One</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
<option value="green">Green</option>
</select>
<label>My favorite Color is:</label>
<input type="text" id="favorite_color" />
</fieldset>
因此,您可以通过javascript
中的以下说明获取所选值:
var color = $("#color option:selected").val();
//Or
var color = $("#color").val();
//Or
var color = $('select[name="color"]').val(); // without reference id