我在表单中有一个提交按钮,并使用PHP脚本来通过电子邮件发送表单。当按下按钮时没有任何反应,甚至没有显示PHP代码,就像没有安装PHP一样。我在机器上测试了其他简单的邮件表单,并显示了代码。这个什么都不做。
HTML代码:
<form name="apply" method="post" action="mail.php">
<font class="error-message"></font>
<div class="form-content">
<div class="left-label">
<label>First and Last name:</label>
<label>School type:</label>
<label>Practice name:</label>
<label class="address">Address:</label>
<label class="address2">Address (cont):</label>
<label class="zip">Zip Code:</label>
<label class="city">City:</label>
<label>Website:</label>
<label>Email:</label>
</div>
<div class="right-fields">
<input type="text" name="fist-last-name" placeholder="First & Last name" title="Please enter First & Last name." />
<select name="select-school">
<option>--select--</option>
<option>Dental Assistant School</option>
<option>Medical Assistant School</option>
<option>Pharmacy Technician School</option>
<!-- <option>Personal Trainer School</option> -->
<option>Other School</option>
</select>
<input type="text" name="practice-name" placeholder="Practice name" title="Please enter your practice name." />
<textarea name="address1" placeholder="Address 1" title="Please enter address1"></textarea>
<textarea name="address2" placeholder="Address 2" title="Please enter address2"></textarea>
<div class="clear"></div>
<input type="text" name="zipcode" placeholder="Zip Code" title="Please enter your zipcode." /><br>
<input type="text" name="city" placeholder="City" title="Please enter the city where you live." />
<input type="text" name="website-address" placeholder="Website" title="Please enter your website address." />
<input type="text" name="email" placeholder="Email" title="Please enter your email address." />
<input type="submit" value="Submit"/>
</div>
</div>
<div class="clear"></div>
</form>
PHP代码,mail.php
:
<?php
$fist_last_name = $_POST["fist-last-name"];
$school_type = $_POST["select-school"];
$practice_name = $_POST["practice-name"];
$address1 = $_POST["address1"];
$address2 = $_POST["address2"];
$zipcode = $_POST["zipcode"];
$city = $_POST["city"];
$website = $_POST["website-address"];
$email = $_POST["email"];
$body = " <table>
<tr><th>First Last name:</th><td>".$fist_last_name."</td></tr>
<tr><th>School type:</th><td>".$school_type."</td></tr>
<tr><th>Practice name:</th><td>".$practice_name."</td></tr>
<tr><th>address1:</th><td>".$address1."</td></tr>
<tr><th>address2:</th><td>".$address2."</td></tr>
<tr><th>Zipcode:</th><td>".$zipcode."</td></tr>
<tr><th>City:</th><td>".$city."</td></tr>
<tr><th>Website:</th><td>".$website."</td></tr>
<tr><th>Email:</th><td>".$email."</td></tr>
</table>";
mail('emailaddress@domain.com','No Reply',$body,"From: noreply@domain.com");
?>
这是表单验证的JavaScript:
/* Form validation */
$("form[name='apply']").submit(function(){
var error = true;
var text_fields = ["fist-last-name", "practice-name", "zipcode", "city", "website-address", "email"];
$.each(text_fields,function(key,value){
if(!$.trim($("input[name='"+value+"']").val()) == "") {
$("input[name='"+value+"']").css({'border-color': '#ccc'});
}
else{
$("input[name='"+value+"']").css({'border-color': 'red'});
error = false;
}
});
if($("form[name='apply'] select[name='select-school']").val() == "--select--") {
$("form[name='apply'] select[name='select-school']").css({'border-color': 'red'});
error = false;
}
else {
$("form[name='apply'] select[name='select-school']").css({'border-color': '#ccc'});
}
var textarea_fields = ["address1", "address2"];
$.each(textarea_fields,function(key,value){
if(!$.trim($("form[name='apply'] textarea[name='"+value+"']").val()) == "" ) {
$("form[name='apply'] textarea[name='"+value+"']").css({'border-color': '#ccc'});
}
else {
$("form[name='apply'] textarea[name='"+value+"']").css({'border-color': 'red'});
error = false;
}
});
console.log(error);
if(error == true) {
$.post('mail.php',$("form[name='apply']").serialize(),function(data){
console.log(data);
});
}
return false;
});
$("form[name='apply'] input[type='text'], form[name='apply'] textarea").blur(function(){
if($.trim($(this).val()) == ""){
$(".error-message").text($(this).attr('title'));
$(this).css({'border-color': 'red'});
}
else {
$(this).css({'border-color': '#ccc'});
$(".error-message").text('');
}
});
$("form[name='apply'] input[type='text'], form[name='apply'] textarea").bind("keydown",function(){
if($.trim($(this).val()) == ""){
$(".error-message").text($(this).attr('title'));
$(this).css({'border-color': 'red'});
}
else {
$(this).css({'border-color': '#ccc'});
}
});
此代码中出现错误:
$("form[name='apply']").submit(function(){
var error = true;
var text_fields = ["fist-last-name", "practice-name", "zipcode", "city", "website-address", "email"];
$.each(text_fields,function(key,value){
if(!$.trim($("input[name='"+value+"']").val()) == "") {
$("input[name='"+value+"']").css({'border-color': '#ccc'});
}
else{
$("input[name='"+value+"']").css({'border-color': 'red'});
error = false;
}
});
if($("form[name='apply'] select[name='select-school']").val() == "--select--") {
$("form[name='apply'] select[name='select-school']").css({'border-color': 'red'});
error = false;
}
else {
$("form[name='apply'] select[name='select-school']").css({'border-color': '#ccc'});
}
var textarea_fields = ["address1", "address2"];
$.each(textarea_fields,function(key,value){
if(!$.trim($("form[name='apply'] textarea[name='"+value+"']").val()) == "" ) {
$("form[name='apply'] textarea[name='"+value+"']").css({'border-color': '#ccc'});
}
else {
$("form[name='apply'] textarea[name='"+value+"']").css({'border-color': 'red'});
error = false;
}
});
console.log(error);
if(error == true) {
$.post('mail.php',$("form[name='apply']").serialize(),function(data){
console.log(data);
});
}
return false;
});
答案 0 :(得分:2)
您的Javascript正在停止提交表单。当您在表单return false
事件中submit
时,它会阻止提交表单。
我想你想要返回error
变量,它似乎是一个布尔值来表示验证错误。
尝试:
$("form[name='apply']").submit(function(){
.... JS code ....
return error;
});
使用它,如果没有验证错误,表单将提交。你的error
布尔值也有点误导,如果error
为真则更合乎逻辑然后存在验证错误,但你反过来 - 对实际的代码执行没有任何影响,只是一个可读性点。
答案 1 :(得分:1)
尝试if(isset($_POST['practice-name'])){ die('values posted'); }else{ die('nothing was posted'}
并告诉我们它的作用。
答案 2 :(得分:1)
首先,尝试在mail.php中注释掉所有内容并做一些非常简单的事情:
<?php echo "This works!"; ?>
只是为了看它是否实际提交到页面。如果是这样,问题将出现在'post'变量或输入表单中。尝试使用输入表单创建一个新的html页面到mail.php,但除了文本框和提交按钮之外别无其他。如果它提交并且mail.php收到所有数据,则问题将与文件以外的其他内容有关。
希望这有帮助!
答案 3 :(得分:0)
既然你说表单根本没有提交 - 我测试了代码并且确实提交了它的方式 - 那么你必须有一些JavaScript函数或事件监听器来阻止表单被提交强>
您的HTML工作和表单提交确实告诉浏览器提交表单。这不能是一个PHP问题,因为如果提交甚至没有发生,那么如果表单本身显示正确,PHP就不会在这里发挥作用。