提交表单而不刷新页面使用jQuery和Ajax不更新MySQL数据库

时间:2010-03-19 21:03:59

标签: php jquery mysql ajax

我是JQuery的新手并且遇到问题,当我单击表单上的提交按钮时,一切都说注册成功但我的MYSQL数据库没有更新一切正常,直到我尝试将JQuery添加到图片中。

有人可以帮我解决这个问题,以便我的数据库更新吗?

由于

这是JQuery代码。

$(function() {

$(".save-button").click(function() {
    var address = $("#address").val();
    var address_two = $("#address_two").val();
    var city_town = $("#city_town").val();
    var state_province = $("#state_province").val();
    var zipcode = $("#zipcode").val();
    var country = $("#country").val();
    var email = $("#email").val();

    var dataString = 'address='+ address + '&address_two=' + address_two + '&city_town=' + city_town + '&state_province=' + state_province + '&zipcode=' + zipcode + '&country=' + country + '$email=' + email;

    if(address=='' || address_two=='' || city_town=='' || state_province=='' || zipcode=='' || country=='' || email=='') {
        $('.success').fadeOut(200).hide();
        $('.error').fadeOut(200).show();
    }

    else
    {
    $.ajax({
    type: "POST",
    url: "http://localhost/New%20Project/home/index.php",
    data: dataString,
    success: function(){
    $('.success').fadeIn(200).show();
    $('.error').fadeOut(200).hide();

   }
});
    }

    return false;

    });
});

这是PHP代码。

if (isset($_POST['contact_info_submitted'])) { // Handle the form.

    // Query member data from the database and ready it for display
    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT users.*, contact_info.*
                                 FROM users 
                                 INNER JOIN contact_info ON contact_info.user_id = users.user_id 
                                 WHERE users.user_id=3");

    $user_id = mysqli_real_escape_string($mysqli, htmlentities('3'));
    $address = mysqli_real_escape_string($mysqli, htmlentities($_POST['address']));
    $address_two = mysqli_real_escape_string($mysqli, htmlentities($_POST['address_two']));
    $city_town = mysqli_real_escape_string($mysqli, htmlentities($_POST['city_town']));
    $state_province = mysqli_real_escape_string($mysqli, htmlentities($_POST['state_province']));
    $zipcode = mysqli_real_escape_string($mysqli, htmlentities($_POST['zipcode']));
    $country = mysqli_real_escape_string($mysqli, htmlentities($_POST['country']));
    $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email']));


//If the table is not found add it to the database
if (mysqli_num_rows($dbc) == 0) {
        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        $dbc = mysqli_query($mysqli,"INSERT INTO contact_info (user_id, address, address_two, city_town, state_province, zipcode, country, email) 
                                     VALUES ('$user_id', '$address', '$address_two', '$city_town', '$state_province', '$zipcode', '$country', '$email')");
}



//If the table is in the database update each field when needed
if ($dbc == TRUE) {
        $dbc = mysqli_query($mysqli,"UPDATE contact_info 
                                     SET address = '$address', address_two = '$address_two', city_town = '$city_town', state_province = '$state_province', zipcode = '$zipcode', country = '$country', email = '$email' 
                                     WHERE user_id = '$user_id'");
}


if (!$dbc) {
        // There was an error...do something about it here...
        print mysqli_error($mysqli);
        return;
}

}

这是XHTML代码。

<form method="post" action="index.php">
    <fieldset>
        <ul>
            <li><label for="address">Address 1: </label><input type="text" name="address" id="address" size="25" class="input-size" value="<?php if (isset($_POST['address'])) { echo $_POST['address']; } else if(!empty($address)) { echo $address; } ?>" /></li>
            <li><label for="address_two">Address 2: </label><input type="text" name="address_two" id="address_two" size="25" class="input-size" value="<?php if (isset($_POST['address_two'])) { echo $_POST['address_two']; } else if(!empty($address_two)) { echo $address_two; } ?>" /></li>
            <li><label for="city_town">City/Town: </label><input type="text" name="city_town" id="city_town" size="25" class="input-size" value="<?php if (isset($_POST['city_town'])) { echo $_POST['city_town']; } else if(!empty($city_town)) { echo $city_town; } ?>" /></li>
            <li><label for="state_province">State/Province: </label>
            <?php

            echo '<select name="state_province" id="state_province">' . "\n";
              foreach($state_options as $option) {
                if ($option == $state_province) {
                  echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
                } else {
                  echo '<option value="'. $option . '">' . $option . '</option>'."\n";
                }
              }
            echo '</select>';

            ?>
            </li>

            <li><label for="zipcode">Zip/Post Code: </label><input type="text" name="zipcode" id="zipcode" size="5" class="input-size" value="<?php if (isset($_POST['zipcode'])) { echo $_POST['zipcode']; } else if(!empty($zipcode)) { echo $zipcode; } ?>" /></li>

            <li><label for="country">Country: </label>
            <?php

            echo '<select name="country" id="country">' . "\n";
              foreach($countries as $option) {
                if ($option == $country) {
                  echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
                } 
                else if($option == "-------------") {
                  echo '<option value="' . $option . '" disabled="disabled">' . $option . '</option>';
                }
                else {
                  echo '<option value="'. $option . '">' . $option . '</option>'."\n";
                }
              }
            echo '</select>';

            ?>
            </li>

            <li><label for="email">Email Address: </label><input type="text" name="email" id="email" size="25" class="input-size" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } else if(!empty($email)) { echo $email; } ?>" /><br /><span>We don't spam or share your email with third parties. We respect your privacy.</span></li>

            <li><input type="submit" name="submit" value="Save Changes" class="save-button" />
                <input type="hidden" name="contact_info_submitted" value="true" />
            <input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
        </ul>
    </fieldset>

</form>

4 个答案:

答案 0 :(得分:0)

您应该考虑重构代码吗? 我可以清理代码的else块,这里是你的代码

else
{
   $.load(
          "http://localhost/New%20Project/home/index.php",dataString,
           function()
           { 
             $('.success').fadeIn(200).show();
             $('.error').fadeOut(200).hide();
           }
         );
}

答案 1 :(得分:0)

将所有jQuery放在

(文档)$。就绪(函数(){ //在这里 });

在表单上添加id。例如id =“myform”。

使用plug-in进行表单验证。要使用插件,请在所有必填字段的输入标记中输入class =“

。”

验证插件的文档是here,因此您可以看到哪个选项符合您的需求。但是,您似乎想要设置submit handler to submit the form via AJAX

$("#myform").validate({
   submitHandler: function(form) {
    $(form).ajaxSubmit();
   }
})

这应该有希望解决你的jQuery问题,如果它适用于常规提交,那么你的服务器端代码就没有问题了。

答案 2 :(得分:0)

答案 3 :(得分:0)

而不是$ .load尝试$ .get因为加载是在$('#result_of_query).load(...)这样的上下文中使用的,其中结果将被加载到'#result_of_query'中,我假设它看起来像是因为这个重装。

所以也许你应该试试这个:

else
{
   $.get(
          "http://localhost/New%20Project/home/index.php",dataString,
           function()
           { 
             $('.success').fadeIn(200).show();
             $('.error').fadeOut(200).hide();
           }
         );
}