2013-04-21 26 views
0

这是我第一次使用jquery通过客户端进行验证,并且遇到了.ajaxSubmit函数(我也刚刚使用.ajax)和当点击提交按钮时,数据库被更新,但ajax函数不会返回结果到页面。所以,当我点击提交按钮时,它看起来没有任何事情发生,但是当我查看数据库时,字段用数据更新。另外,当我注释掉ajax函数并仅使用警报时,当我点击提交按钮时,弹出警报。如果有人可以请帮助我,因为我已经为此工作了一周。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>"} 

回答

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个工作。猜猜,我回到了寻找这个问题的绘图板上。感谢您的建议爆炸药丸和穆萨。

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

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); 
+0

谢谢,我从response.php文件中删除了所有html,但问题仍然存在,请参阅上面的更新。 – nelly 2013-04-22 15:01:14

1

正在可能返回由于对$return一个未定义的数组索引msg的通知的发射无效JSON,但更可能因为很多HTML的发射的。本质上,这将返回:

<some html>JSON<some html> 

这不是有效的JSON,所以jQuery将不会调用success回调。删除全部为的HTML,并发出只有的JSON。

+0

谢谢,我从response.php文件中删除了所有html,但问题仍然存在,请参阅上面的更新。我假设数据的循环发生,因为'$返回'是未定义在我的PHP文件? – nelly 2013-04-22 14:58:59

相关问题