我有一个带有表单的模态窗口,它应该将字段数据提交到数据库,而不是关闭模态窗口,然后在淡出模态窗口之前显示“你已被RSVP”。
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"')");
?>
答案 0 :(得分:0)
我认为您的 ID 是数据库中的自动增量。而不是
mysql_query("INSERT INTO rsvp-yes (ID, email, city) VALUES ('NULL', '".$email"', '".$city"')");
使用
mysql_query("INSERT INTO rsvp-yes (ID, email, city) VALUES ('', '".$email"', '".$city"')");
删除NULL
答案 1 :(得分:-1)
您应该使用mysql_real_escape_string()
函数来防止SQL注入。它还可以防止因引用或双引号而导致插入数据时出错。
$email = $_POST['email'];
$email=mysql_real_escape_string($email);
$city = $_POST['city'];
$city=mysql_real_escape_string($city);
希望您的ID
列是主键,还有自动增加字段。所以你不应该在你的sql语句中包含ID
。
mysql_query("INSERT INTO rsvp-yes (email, city)
VALUES ('$email', '$city')");