Json将空数据回显给AJAX

时间:2014-03-31 17:37:45

标签: php jquery sql ajax json

运行两个sql命令后,JSON应该将更新的结果发送到AJAX脚本。触发后,会在页面中打开一个数据应该填充的位置,但没有任何内容。下面的PHP不会创建任何错误消息,并且数据正在更新到数据库。我希望我有一个更具体的问题,但我找不到问题的原因。

        $storyidr=$_POST['storyidr'];
        $mysqli = mysqli_connect($dbhost,$dbusername,$dbpasswd,$database_name) or die ("Couldn't connect to server.");
         if (mysqli_connect_errno($mysqli))
           {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
           }
        $sql  = "INSERT INTO ratings (storyidr, rank, entry_date) VALUES ('$_POST[storyidr]','$_POST[value]',now());"; 
        $sql .= "SELECT AVG(rank) AS avrank from ratings WHERE storyidr = $storyidr";
        if($mysqli->multi_query($sql))
        {   
        if ($result = $mysqli->store_result())
        { 
        $data = mysqli_fetch_assoc($result);
        $avrank = $data['avrank'];
        if(is_null($avrank)){$avrank ="null";}
                if(!$mysqli)
                {
                      $arr = array ('status'=>'fail');
                      echo json_encode($arr);
                }
                else
                {


                      echo json_encode($avrank);
                }
                        exit;
        }
        }

AJAX

    <script type ="text/javascript">
         $('#products .rateit').bind('rated reset', function (e) {
             var ri = $(this);
     var value = ri.rateit('value');
             var storyidr = ri.data('storyidr');
              ri.rateit('readonly', true);
             $.ajax({
                 url: '../rate.php',
                 data: { storyidr: storyidr, value: value }, 
                 type: 'POST',
                 success: function (data) {
                     $('#response').append('<li>' + data + '</li>');
                 },
                 error: function (jxhr, msg, err) {
                     $('#response').append('<li style="color:red">' + msg + '</li>');
                 }
             });
         });
    </script>

HTML

     <ul id="response"> 
     </ul>

0 个答案:

没有答案