【发布时间】:2014-06-26 00:09:24
【问题描述】:
我的这段代码运行良好,除了它将发布来自 php 的所有结果,我只需要特定的 json_encoded 数据。这是从我的数据库中提取的 PHP。
<?php
session_start();
$_SESSION['charname'] = 'Egahtrac';
require_once ('mysqli_connect.php');
$q = "SELECT * FROM dps WHERE charname='" . $_SESSION['charname'] . "';";
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$dmg = $row['dmg'];
$curr_hp = $row['curr_hp'];
$tot_hp = $row['tot_hp'];
$attk_spd = $row['attk_spd'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['attack'])) {
if ($curr_hp > 0) {
$row['curr_hp'] = $row['curr_hp'] - $row['dmg'];
$q = "UPDATE dps SET curr_hp='" . $row['curr_hp'] . "' WHERE charname='" . $_SESSION['charname'] . "';";
$r = mysqli_query($dbc, $q);
echo json_encode($row);
}
}
}
这里是 jQuery
$('input#attack').on('click', function() {
$.post('dpsloop2.php', { attack: true }, function(data) { $('span#curr_hp').text(data.dmg); });
});
// see here that i'm just trying to test if it works by echo'ing the dmg value to the curr_hp span to see if it changes.
$('#dpsform').submit( function() {
return false;
});
我知道这与 .text(data) 部分有关。但是,如果我尝试做 .text(data.curr_hp) 它不起作用。我检查了 json 数组是否从 php 回显了正确的格式,那么为什么我不能从 JSON 数组中访问该 data.curr_hp 呢?
【问题讨论】:
标签: php jquery mysql ajax json