表单在页面加载上发送结果

时间:2014-02-09 19:38:23

标签: javascript php jquery forms

我有一个隐藏的div我点击提交按钮加载。表单也在页面加载上发送结果?我确保我包含了validateForm()函数,并在return函数的末尾调用它,返回validateForm();它也正在加载隐藏的div并在我点击提交后立即发送结果。我做错了什么?

<?php
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$email = $_POST['email'];

$name = stripslashes($name);
$address = stripslashes($address);
$city = stripslashes($city);
$state = stripslashes($state);
$phone = stripslashes($phone);
$email = stripslashes($email);


$to  = 'info@thebrlab.com ' . ', ';
$to .= $Email; 

$from = "$Email ";
$subject = 'Look and Learn: Applicant'; 
$message = <<<EOF
<html>
<body bgcolor="#FFFFFF">

<b>Look and Learn: Applicant</b><br /><br />

<b>Name:</b> $name<br />
<b>Address:</b> $address / $city, $state<br />
<b>Phone:</b> $phone<br />
<b>Email:</b> $email<br />

</body>
</html>
EOF;
//end of message

// Additional headers
$headers .= 'From: Razor Chic of Atlanta <info@thebrlab.com>' . "\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";

mail($to, $subject, $message, $headers);

?>


<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Sign Up</title>
<link rel="stylesheet" href="css/sign-up.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">


$(document).ready(function() {
$('#submit').click(function() {
$('#thankyou').show();
$("#hidden1").html($("#thankyou").html());
});
});

</script>

<script>

function validateForm()
{

var x=document.forms["myContact"]["name"].value;
if (x==null || x=="")
{
alert("Please Enter Your Full Name");
return false;
}

var x=document.forms["myContact"]["address"].value;
if (x==null || x=="")
{
alert("Please Enter Your Address");
return false;
}

var x=document.forms["myContact"]["city"].value;
if (x==null || x=="")
{
alert("Please Enter Your City");
return false;
}

var x=document.forms["myContact"]["phone"].value;
if (x==null || x=="")
{
alert("Please Enter Your Phone Number");
return false;
}

var x=document.forms["myContact"]["email"].value;
if (x==null || x=="")
{
alert("Please Enter Your Email");
return false;
}

}


$('#myContact').submit(function () {

$.each($('#myContact .notShouldBeBlank'), function()
{
if($(this).val() == ''){
$(this).after('<span class="error">This field is required.</span>');
return false;
}
});

return validateForm();
}
</script>


</head>
<body style="overflow: hidden; overflow-x: hidden; overflow-y: hidden;">



<div id="wrap">
<div id="hidden1"></div>
<div style="font-size: 18px; font-weight: bold; font-family: Verdana, Geneva, sans-serif;">
Sign-Up: Look And Learn Class
</div>

<br>

<form id="myContact" action="" name="myContact" method="post" enctype="multipart/form-data">
<div>
<label>
<span>Name: *</span><br>
<input name="name" type="text" size="64" placeholder="Name">
</label>
</div>

<div>
<table width="100%" >

<tr>

<td width="230">
<label>
<span>Address: *</span><br>
<input placeholder="Address" size="100" type="text" name="address" maxlength="100">
</label>
</td>

<td width="160">
<label>
<span>City *</span><br>
<input placeholder="City" name="city" type="text" id="city" maxlength="100" />
</label>
</td>

<td width="189">
<label>
<span>State *</span><br>
<input placeholder="State" name="city" type="text" id="city" maxlength="3" />
</label>
</td>
</tr>

</table>
</div>

<div>
<label>
<span>Phone: *</span><br>
<input placeholder="Phone" size="64" type="text" name="phone">
</label>
</div>


<div>
<label>
<span>Email: *</span><br>
<input placeholder="Email address" size="64" type="email" name="email">
</label>
</div>

<div>
<button name="submit" type="submit" id="submit">S  I  G  N    U  P</button>
</div>
</form>
<p>Note: * Fields are required</p>

</div>


<!---- THANK YOU---->
<?php
if($sent){

echo '<div id="thankyou" style="display:block;">';
}
else{
echo '<div id="thankyou" style="display:none;">';
}
?>


<!---- PAY BEGINS ---->
<div id="paynow1-wrapper">
<div id="paynow1">

<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="business" value="info@thebrlab.com">
<input type="hidden" name="item_name" value="Look and Learn: Deposit">
<input type="hidden" name="amount" value="100.00">            
<input type="hidden" name="return" value="http://thebrlab.com/razor-chic-of-atlanta/thank-you.html">
<input type="hidden" name="undefined_quantity" value="1">

<input style="background: none" onMouseOver="this.src='images/pay-now-up.png'" onMouseOut="this.src='images/pay-now-down.png'" type="image" src="images/pay-now-down.png" height="41" width="141" border="0" alt="Pay Now" class="button">

</form>

</div>
</div> 
<!---- PAY ENDS ---->


<!---- PAY BEGINS ---->
<div id="paynow2-wrapper">
<div id="paynow2">

<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="business" value="info@thebrlab.com">
<input type="hidden" name="item_name" value="Look and Learn: Balance">
<input type="hidden" name="amount" value="99.00">            
<input type="hidden" name="return" value="http://thebrlab.com/razor-chic-of-atlanta/thank-you.html">
<input type="hidden" name="undefined_quantity" value="1">

<input style="background: none" onMouseOver="this.src='images/pay-now-up.png'" onMouseOut="this.src='images/pay-now-down.png'" type="image" src="images/pay-now-down.png" height="41" width="141" border="0" alt="Pay Now" class="button">

</form>

</div>
</div> 
<!---- PAY ENDS ---->



<!---- PAY BEGINS ---->
<div id="paynow3-wrapper">
<div id="paynow3">

<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="business" value="info@thebrlab.com">
<input type="hidden" name="item_name" value="Look and Learn: Full Payment">
<input type="hidden" name="amount" value="199.00">            
<input type="hidden" name="return" value="http://thebrlab.com/razor-chic-of-atlanta/thank-you.html">
<input type="hidden" name="undefined_quantity" value="1">

<input style="background: none" onMouseOver="this.src='images/pay-now-up.png'" onMouseOut="this.src='images/pay-now-down.png'" type="image" src="images/pay-now-down.png" height="41" width="141" border="0" alt="Pay Now" class="button">

</form>

</div>
</div> 
<!---- PAY ENDS ---->



<img src="images/thank-you/look-and-learn1.png" />

</div>
<!---- THANK YOU---->


</body>
</html>

1 个答案:

答案 0 :(得分:2)

你问,“表单也在页面加载上发送结果?”,如果您的意思是页面加载mail()正在执行那么你就是正确的。通常的模式是测试是否设置了某个请求变量,然后才调用mail(),例如

if (isset($_POST['name'])) {
    mail($to, $subject, $message, $headers);
}