jquery - How do I post specific json data from php/mysql instead of all data -
i have code working pretty except it's going post of results php want specific json_encoded data. here's php pulls database.
<?php session_start(); $_session['charname'] = 'egahtrac'; require_once ('mysqli_connect.php'); $q = "select * dps 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'] . "' charname='" . $_session['charname'] . "';"; $r = mysqli_query($dbc, $q); echo json_encode($row); } } }
here's jquery
$('input#attack').on('click', function() { $.post('dpsloop2.php', { attack: true }, function(data) { $('span#curr_hp').text(data.dmg); }); }); // see here i'm trying test if works echo'ing dmg value curr_hp span see if changes. $('#dpsform').submit( function() { return false; });
i know has .text(data) part. if tried .text(data.curr_hp) doesn't work. i've checked see json array echoing proper format php, why can't access data.curr_hp json array?
in php should call echo json_encode(something)
once. json response has single object or array; if want return multiple things, need put them in array. should do:
echo json_encode($curr_hp);
then need use datatype
argument $.post()
tell response json. parse you.
$.post('dpsloop2.php', {attack: true }, function(data) { $('span#curr_hp').text(data); }, 'json');
if want return whole row, should be:
echo json_encode($row);
and js should like:
$.post('dpsloop2.php', {attack: true }, function(data) { $('span#curr_hp').text(data.curr_hp); $('span#dmg').text(data.dmg); }, 'json');
Comments
Post a Comment