[英]AJAX post jQuery to PHP $_POST
我有這個模態jQuery AJAX:
$('#switch_modal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).attr('data-id');
$.ajax({
type : 'post', // commented for this demo
url : 'pars.php', // commented for this demo
data : 'id='+ rowid,
success : function(data) {
$('.fetched-data').show().html(rowid); // show rowid for this demo
}
});
});
我的mysql查詢:
$query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC";
while ($row = $result->fetch_assoc()) {
我的模態數據ID:
<a href="#viewgame" data-toggle="modal" data-id="<?php echo $row['id'];?>"">
我該如何像php post一樣使用var rowid? 像這樣:
$id = $_POST['rowid'];
echo $id;
如果我理解得很好,請測試您的操作...
您的JavaScript rowid
是值。 $_POST
標識符是id
。
在您的PHP中嘗試一下:
$id = $_POST['id'];
echo $id;
您將通過ajax獲取以POST值發送的javascript rowid
(名為$_POST['id']
)...
並返回有關ajax成功的data
。
$('.fetched-data').show().html(data);
因此,您將不得不在jQuery html()
使用data
。Wich是回顯的文本。
-----
接受此答案后編輯
(根據您的最新評論)
所以我有這個查詢:
$query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC"; if ($result = $conn->query($query)) { while ($row = $result->fetch_assoc()) { $gameid = $row['id']; } }
所以我想在此查詢中使用$ gameid變量:
$sql = "SELECT * FROM games WHERE id='".$gameid."'";
我了解,您想獲得最后匹配的完整行 ,其中游戲表中的獲勝者值為空。
不需要ajax調用...
無需第二次查詢。
去做就對了:
$query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC";
$result = $conn->query($query);
$row = $result->fetch_assoc();
for ($i=0;$i<sizeOf($row);$i++){ // This is the «size» (number of values) of one row, the last fetched.
echo $row[$i] . "<br>";
}
您將得到所有行值的回顯...
這將是最后獲取的匹配行。
如果您有許多匹配的行並想要所有結果,請按以下步驟進行:
$query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC";
$result = $conn->query($query));
while ($row = $result->fetch_assoc()) { // While fetching, echo all values of one matching line.
echo "row id: " . $row['id'] . "<br>";
echo "values: <br>";
for ($i=0;$i<sizeOf($row);$i++){
echo $row[$i] . "<br>";
}
}
請注意 ,我建議使用此腳本來啟發您了解while fetch loop
可能結果。 您需要做一些工作才能使其正確顯示在頁面上。
;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.