0

这是我使用 jquery 通过客户端验证的第一个表单,我遇到了 .ajaxSubmit 函数的问题(我也只是使用了 .ajax),当单击提交按钮时,数据库已更新,但 ajax 函数没有t 将结果返回到页面。因此,当我单击提交按钮时,它看起来好像什么都没有发生,但是当我查看数据库时,字段会随数据更新。另外,当我注释掉 ajax 函数并只使用警报时,当我单击提交按钮时,会弹出警报。如果有人可以帮助我,因为我已经为此工作了一周。

这是 html 和 jQuery 脚本:

<html>
<head>
<style type="text/css">
<!--
@import "./css/job.css";
-->
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="./js/jquery.validate.js"></script>
<script type="text/javascript" src="./js/jquery.form.js"></script>
<script type="text/javascript">
(function($,W,D) {
    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function() {
            //form validation rules
            $("#job").validate({
                 rules: {
                        jobtype: {
                        required: true },
                        account: {
                        required: true,
                        minlength: 8 },
                        phone: {
                        required: true,
                        minlength: 7 },
                        comment: {
                        required: true,
                        minlength: 5 },
                        available: {
                        required: true,
                        minlength: 3 }
                        },
                messages: {
                        jobtype: {
                        required: "Select a job type" },
                        account: {
                        required: "Enter account in correct format" },
                        phone: {
                        required: "Enter phone number" },
                        comment: {
                        required: "Enter WIP details" },
                        available: {
                        required: "Enter an available timeframe" }
                        },
                submitHandler: function(form) {
//                        alert("Submitting Job");
                        $(form).ajaxSubmit({
                                        url: 'response.php',
                                        type: 'POST',
                                        data: {
                                        jobtype: $("#jobtype").val(),
                                        account: $("#account").val(),
                                        phone: $("#phone").val(),
                                        comment: $("#comment").val(),
                                        available: $("#available").val(),
                                        },
                                        dataType: 'json',
                                                cache: false,
                                        timeout: 7000,
                                        success: function(data) {
                                        $('form #schedTable').html(data.msg).fadeIn('fast');
                                }
                        });
                }
            });
        }
    }
    //when the dom has loaded setup form validation rules
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });
})(jQuery, window, document);
</script>
</head>
<body>
<div id="jobForm">
<form id="job" name="job" action="response.php" method="post" novalidate="novalidate">
<h1>Job Scheduling</h1>
<label>Type</label>
<select id="jobtype" name="jobtype">
  <option value="" selected>&lt;&lt; SELECT &gt;&gt;</option>
  <option value="service">Service</option>
  <option value="install">Install</option>
</select>
<label>Account</label>
<input type="text" id="account" name="account" maxlength="10" size="10">
<label>Phone</label>
<input type="text" id="phone" name="phone" maxlength="7" size="7">
<label>Comment</label>
<textarea id="comment" name="comment" rows="2" cols="40" maxlength="40"></textarea>
<label>Available</label>
<input type="text" id="available" name="available" maxlength="20" size="20">
<input id="submit" name="submit" type="submit" value="Submit" class="submit">
</form>
</div>

<div id="schedTable"></div>

</body>
</html>

这是 response.php 代码:

<?php

$mydb connection info here

?>

<!DOCTYPE HTML>
<html>
<head>
<title>Test Form</title>
<link rel="shortcut icon" href="/favicon.ico">
</head>
<body>

<?php

// form inputs
$account = trim($_POST['account']);
$type = $_POST['type'];
$phone = $_POST['phone'];
$comment = trim($_POST['comment']);
$available = trim($_POST['available']);

// Insert into mysql db
$ins = mysql_query(sprintf("INSERT INTO table (type,comment,available,phone,account) VALUES ('%s','%s','%s','%s','%s')", $type,$comment,$available,$phone,$acct)) or die(mysql_error());

$sched = mysql_query(sprintf("SELECT * FROM table ORDER BY type asc"));

$return['msg'] = "
    <table border='1'>
    <tr>
    <th>Type</th>
    <th>Account</th>
    <th>Comment</th>
    </tr>";

    while($rowu = mysql_fetch_array($sched)) {
    $return['msg'] .= "
        <tr>
        <td>{$type}</td>
        <td>{$acct}</td>
        <td>{$comment}</td>
        </tr>";
        }
        $return['msg'] .= "</table>";

       // header("Content-Type: text/javascript; charset=utf-8");
       // $return['msg'] = "Testing " .$account . "works";

        header('Content-Type: application/json');
        echo json_encode($return);

?>
</body>
</html>

更新:我删除了所有的 html 并且在使用 chrome 时问题是一样的。当我使用 IE 8 并单击提交按钮时,它要求打开或另存为文本文件(也多次重复相同的表单数据):

{"msg":"<table border='1'><tr>\n    <th>Type<\/th>\n    <th>Account<\/th>\n    <th>Comment<\/th>\n    <\/tr><tr>\n        <td>other<\/td>\n        <td>12345678<\/td>\n        <td>sssss<\/td>\n   <\/tr><tr>\n      <td>other<\/td>\n        <td>12345678<\/td>\n        <td>sssss<\/td>\n      <\/tr><tr>\n        <td>other<\/td>\n        <td>12345678<\/td>\n        <td>sssss<\/td>\n      <\/tr><\/table>"}
4

3 回答 3

1

去掉 php 逻辑周围的所有 html。这导致了问题,响应需要只有 json 而不是 html + json + html。

<?php

$mydb connection info here    

// form inputs
$account = trim($_POST['account']);
$type = $_POST['type'];
$phone = $_POST['phone'];
$comment = trim($_POST['comment']);
$available = trim($_POST['available']);

// Insert into mysql db
$ins = mysql_query(sprintf("INSERT INTO table (type,comment,available,phone,account) VALUES ('%s','%s','%s','%s','%s')", $type,$comment,$available,$phone,$acct)) or die(mysql_error());

$sched = mysql_query(sprintf("SELECT * FROM table ORDER BY type asc"));

$return['msg'] = "
    <table border='1'>
    <tr>
    <th>Type</th>
    <th>Account</th>
    <th>Comment</th>
    </tr>";

    while($rowu = mysql_fetch_array($sched)) {
    $return['msg'] .= "
        <tr>
        <td>{$type}</td>
        <td>{$acct}</td>
        <td>{$comment}</td>
        </tr>";
        }
        $return['msg'] .= "</table>";

       // header("Content-Type: text/javascript; charset=utf-8");
       // $return['msg'] = "Testing " .$account . "works";

        header('Content-Type: application/json');
        echo json_encode($return);
于 2013-04-21T23:15:16.277 回答
1

返回无效的 JSON 可能是由于发出了未定义数组索引的通知msg$return但更可能是因为发出了大量 HTML。基本上这会返回:

<some html>JSON<some html>

这不是有效的 JSON,因此 jQuery 不会调用success回调。删除所有HTML 并发出JSON。

于 2013-04-21T23:16:00.107 回答
0

响应现在有效。我将代码更改为以下内容,结果现在显示在我的 schedTable div 中,无需刷新页面:

submitHandler: function(form) {
     var data = $("#job").serialize();
     $.ajax({
     type: 'POST',
     url: 'response.php',
     data: data,
     cache: false,
     success: function(data) {  
          $('#job')[0].reset();
          $('#schedTable').html(data).fadeIn('fast');
     }
     });
}

但是,现在显示的结果并不完全正确。比如说,我通过表单提交了 3 个作业,只有第三个作业会显示 3 次,而不是数据库中显示的所有 3 个作业。猜猜,我回到绘图板上搜索这个问题。感谢 Explosion Pills 和 Musa 的建议。

更新:将 while 循环更改为 json 数组方法解决了此问题。现在一切正常。

于 2013-04-22T19:15:10.457 回答