繁体   English   中英

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

[英]Ajax and PHP return data to table cell

PHP / MySQL的新手在这里,我一直在尝试学习一些Ajax。

我想将一个JSON(一个数字,例如12345 )返回到$variable并在单个表单元格中echo此变量。 (也许有更好的方法可以做到这一点,但是我只想知道它是如何做到的-如果可能的话!)

到目前为止,我可以使用以下代码检索JSON图形并将其显示在<div id="result"></div>中,因此我知道请求有效-我只是停留在传递变量部分。

我的回复(在Chrome中)如下所示;

在此处输入图片说明

到目前为止,我的代码是;

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

?>

我的JS (包含在上面的chartAjax.php页面中)

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

抱歉,如果代码/方法非常业余,欢迎任何帮助或建议!

您正在选择一个具有ID结果的元素以显示您的回复,请更新td ID

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



<table id="datatable" class="table">
    <tbody>
      <tr>
        <th>Data</th>
        <td id="result">{RETURNED JSON FIGURE HERE}</td> <!-- stuck here -->
      </tr>
    </tbody>
</table>

尝试这个

添加此(AS计数)

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

然后应用这个

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

然后在你的

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM