-2

我有一个带有表单的模态窗口,它应该将字段数据提交到数据库,而不是关闭模态寡妇,然后在淡出模态窗口之前显示“您已被回复”。

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"')");

?>
4

2 回答 2

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

于 2012-12-13T06:12:26.113 回答
-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列是主键,也是自动递增的字段。所以你不应该包含ID在你的 sql 语句中。

mysql_query("INSERT INTO rsvp-yes (email, city)
VALUES ('$email', '$city')");
于 2012-12-13T05:33:26.120 回答