2015-08-20 30 views
0

這裏新增了PHP/MySQL,我一直在嘗試學習一些Ajax。Ajax和PHP將數據返回到表單元格

我想在一個表格單元格返回一些JSON(一張圖e.g 12345)到$variableecho這個變量英寸(可能有更好的方法來做到這一點,但我只是想知道它是如何完成的 - 如果可能的話)

到目前爲止,我可以檢索JSON圖並使用下面的代碼在<div id="result"></div>中顯示它,所以我知道請求的作品 - 我只是在傳遞可變部分。

我的回覆(在Chrome中)看起來像這樣;

enter image description here

我的代碼到目前爲止;

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> 

如果代碼/方法是非常業餘的道歉,歡迎任何幫助或建議!

+0

你的數據/響應是什麼樣的? – Jai

+0

這不是json,這只是文字。 json是像'{key:value}'這樣的關鍵值對。 – Jai

回答

0

試試這個

添加這個(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> 
+0

你可以打印你的$數據嗎? – Ponce

+0

我已經更新了我的答案。請檢查。 – Ponce

+0

你可以從你的ajax得到它到你的迴應td嗎?啊,你是對的。但對我來說,我通常只是返回它,因爲我將值返回給調用者。 – Ponce

0

您選擇的是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>