【问题标题】:Submiting a Form without Refreshing the page with jQuery and Ajax Not updating MySQL database提交表单而不使用 jQuery 和 Ajax 刷新页面 不更新 MySQL 数据库
【发布时间】:2010-03-19 21:03:59
【问题描述】:

我是 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>

【问题讨论】:

  • 你的 ajax 调用工作正常吗? ..使用firebug并查看发布的值和响应...也可能有一些mysql错误(可能是拼写错误或其他).. mssql_rows_affected可能对你有帮助

标签: php jquery mysql ajax


【解决方案1】:

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

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

【讨论】:

  • 您的代码让我可以更新我的 MySQL 数据库,但现在它不允许我在不刷新页面的情况下提交表单?
【解决方案2】:

把你所有的jQuery放在里面

$(document).ready(function(){ //这里的东西 });

在表单上添加一个 id。例如 id="myform"。

使用plug-in 进行表单验证。要使用该插件,请将 class="required 放在所有必填字段的输入标签中。"

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

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

这有望解决您的 jQuery 问题,如果它适用于常规提交,则您的服务器端代码没有问题。

【讨论】:

    【解决方案3】:
    $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");
    
    
    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'");
    }
    

    mysql(i)_query 永远不会返回 true,它只会在成功时返回资源或在失败时返回 FALSE。

    这可能是你想要做的更多

    if ($dbc !== FALSE) {
      // do update query
    

    【讨论】:

      【解决方案4】:

      而不是 $.load 尝试 $.get 因为 load 被用于像 $('#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();
                 }
               );
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 2021-04-28
        • 2020-10-13
        相关资源
        最近更新 更多