javascript - How to "highlight" a div based on a query result? -
i'm developing little aplication game. application consist in showing given monster. have database monster, maps , maps monsters belong.
right application working, if user write "poring" example, list in html appear map name , map id.
anyway, have grid whole world of game, every square in grid map, , each grid have specific id.
so, lets i'm looking "poring" in application.
right show following list:
map: prontera field 07 map_id: prt_fild07 map: prontera field 08 map_id: prt_fild08
but want not show list, highlight divs (or add kind of css) divs id="prt_fild07" , id="prt_fild08".
i know need use ajax i'm not sure how, since i'm learning jquery anyway, right i'm using ajax display list.
here's jquery code:
$('#ro-form').submit(function(e){ e.preventdefault(); //prevent default submission $.ajax({ url: 'files/php/process.php', type: 'post', data: $(this).serialize(), //serielize form data datatype: 'html' }) .done(function(data){ $('#test').fadeout('slow',function(){ $('#test').fadein('slow').html(data); }); }) .fail(function(){ alert(':('); }); });
and here php part
<?php include 'connection.php'; if( $_post ){ $mob_id_name = $_post['mob_id_name']; } $sql = "select restartmap.map.map_name, restartmap.map.map_id restartmap.map, restartmap.map_monster, restartmap.monster restartmap.map.map_id = restartmap.map_monster.map_monster_map_id , restartmap.monster.monster_id = restartmap.map_monster.map_monster_monster_id , (restartmap.monster.monster_id = '$mob_id_name' or restartmap.monster.monster_name '$mob_id_name')"; $result = $conn->query($sql); $conn->close(); ?> <div id="result-container"> <?php if($result->num_rows > 0 ){ while($row = $result->fetch_assoc()){ print "map: " . $row["map_name"] . " map_id: " .$row["map_id"] . "<br>"; } }else{ print "<p> nothing :( </p>"; } ?> </div>
basically i'm doing sending index.php information process.php via ajax , printing whole $result var in index.php want highlight divs same id map_id.
you change jquery code accept json
datatype
, so, in php use parse_str
parse out posted serialized data array can work with, build array returned parameter in .done
jquery ajax function. using $return
in php below, json_encode
value can used.
something following below should work nicely. didn't specify want inside of divs, put put in here
text in place of whatever need in there.
jquery:
$('#ro-form').submit(function(e){ e.preventdefault(); //prevent default submission var $parentdiv = $('<div />').attr('id', 'result-container'); $.ajax({ url: 'files/php/process.php', type: 'post', data: { formdata: $(this).serialize() }, datatype: 'json' }) .done(function(data){ if (data.hasownproperty('msgids')) { for(var x = 0, len = data['msgids'].length; x < len; x++) { var $div = $('<div />').attr('id', data['msgids'][x]).text('put in here'); $div.appendto($parentdiv); } $('body').append($parentdiv); } }) .fail(function(){ alert(':('); }); });
php:
<?php $return = array(); include 'connection.php'; if(!empty($_post['formdata'])) { parse_str($_post['formdata'], $data); $mob_id_name = $data['mob_id_name']; $sql = "select restartmap.map.map_name, restartmap.map.map_id restartmap.map, restartmap.map_monster, restartmap.monster restartmap.map.map_id = restartmap.map_monster.map_monster_map_id , restartmap.monster.monster_id = restartmap.map_monster.map_monster_monster_id , (restartmap.monster.monster_id = '$mob_id_name' or restartmap.monster.monster_name '$mob_id_name')"; $result = $conn->query($sql); $conn->close(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()){ $return['msgids'][] = $row['map_id']; } } } echo json_encode($return); die(); ?>
Comments
Post a Comment