douju3911 2015-08-20 10:10
浏览 133
已采纳

Ajax和PHP将数据返回到表格单元格

New to PHP/MySQL here and I have been trying to learn some Ajax.

I want to return some JSON (one figure e.g 12345) to a $variable and echo this variable in in a single table cell. (There may be better ways to do this however I just want to know how it's done - if at all possible!)

So far I can retrieve the JSON figure and display it within a <div id="result"></div> using the code below, so I know the request works - I'm just stuck at passing the variable part.

My response (in Chrome) looks like this;

enter image description here

My code so far is;

chartAjax.php

<form method="post" id="search">
  <input type="text" id="date" name="date">
  <input type="submit" value="Submit">
</form>

<div id="result"></div> <!-- this successfully displays the figure on submit -->

<table id="datatable" class="table">
    <tbody>
      <tr>
        <th>Data</th>
        <td>{NEED TO echo the $result here}</td> <!-- stuck here -->
      </tr>
    </tbody>
</table>

monthConn.php

<?php 

/* set header type to json */
header('Content-Type: application/json');

/* array to pass back data */
$data = array();

/* get date from form submit */
if (isset($_POST['date']) && !empty($_POST['date'])){$date = $_POST['date'];}

/* Create a prepared statement */
$stmt = $conn -> prepare("SELECT count(*) FROM transactions WHERE TimeStamp >= ?");

/* Bind parameters */
$stmt -> bind_param("s", $date);

/* Execute it */
$stmt -> execute();

/* Bind results */
$stmt -> bind_result($data);

/* fetch values */
$stmt->fetch();

 /* close conn */
$stmt->close();

echo json_encode($data);

?>

My JS (contained within chartAjax.php page above)

<script>
$(document).ready(function() {

    $('#search').submit(function(event) {

        event.preventDefault();

        /* Clear result div*/
        $("#result").html('');

        /* Get from elements values */
        var values = $(this).serialize();

        ajaxRequest= $.ajax({
            url: "monthConn.php",
            type: "post",
            data: values,
            success: function(response, data) {
            if(data == "success") {
              $("#result").html(response); //add response to results div
            }
            },
        });
    });
});
</script>

Apologies if the code/methodology is very amateur, any help or suggestions would be welcome!

  • 写回答

2条回答 默认 最新

  • dongtang1918 2015-08-20 10:33
    关注

    Try this

    add this (AS count)

    $stmt = $conn -> prepare("SELECT count(*) AS count FROM transactions WHERE TimeStamp >= ?");
    

    then apply this

    //just incase you want to return more than 1 value in the future
    return json_encode(array('count' => $data));
    

    then in your

    if(data == "success") {
        var return_data = $.parseJSON(response);
    
        $("#datatable").html(return_data['count']);
    }
    
    <table id="datatable" class="table">
        <tbody>
          <tr>
            <th>Data</th>
            <td id="response"></td>
          </tr>
        </tbody>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)