我有一个带有表单的模态窗口,它应该将字段数据提交到数据库,而不是关闭模态寡妇,然后在淡出模态窗口之前显示“您已被回复”。
2件事:
1)提交时,模式窗口关闭?!它应该保持打开状态 2)数据没有插入到数据库中。
模态窗口 HTML
<form id="rsvp-yes" action="#" method="post" name="rsvp-yes">
<label for="email">Your E-mail</label>
<input id="email" class="txt" type="email" name="email" />
<label for="location">Choose your location</label>
<select name="city" id="city">
<option value="None Selected" selected>Select</option>
<option value="Perth">Perth</option>
<option value="Brisbane">Brisbane</option>
<option value="Sydney">Sydney</option>
<option value="Melbourne">Melbourne</option>
</select>
<button id="rsvp">RSVP</button></form></div>
JAVASCRIPT
$(document).ready(function() {
$(".modalbox").fancybox();
$("#rsvp").submit(function() { return false; });
function validateEmail(email) {
var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return reg.test(email);
}
$("#rsvp").on("click", function(){
var emailval = $("#email").val();
var city = $("#city").val();
var mailvalid = validateEmail(emailval);
if(mailvalid == false) {
$("#email").addClass("error");
}
else if(mailvalid == true){
$("#email").removeClass("error");
}
if(mailvalid == true) {
//&& msglen >= 4
// if both validate we attempt to send the e-mail
// first we hide the submit btn so the user doesnt click twice
$("#rsvp").replaceWith("<em>RSVP'ing you...</em>");
$.ajax({
type: 'POST',
url: 'rsvp-yes.php',
data: $("#rsvp-yes").serialize(),
success: function(data) {
if(data == "true") {
$("#rsvp-yes").fadeOut("fast", function(){
$(this).before("<p><strong>You have been RSVP'd</strong></p>");
setTimeout("$.fancybox.close()", 1000);
});
}
}
});
PHP
<?php
$email = $_POST['email'];
$city = $_POST['city'];
mysql_connect(myhost,myuser,mypass) or die("Could not connect: " .
mysql_error());
mysql_query("USE mydbname");
mysql_query("INSERT INTO rsvp-yes (ID, email, city)
VALUES ('NULL', '".$email"', '".$city"')");
?>