javascript - How to fetch data from database with json_encode of ajax -
i use ajax fetch data db. , chose alert(valdata) in success function test data, unlucky nothing return ajax. tested
select contact idc id='5';
it works fine in mysql cmd line.
here js code:
var stnum = 5; $.ajax({ datatype:'json', type:"post", url:"get_ajax_csc.php", data:{stnum:stnum}, success:function(data) { var valdata = data; alert(valdata); $('#stcontact').val(data.stcnt); $('#stphone').val(data.stpho); } });
here html code:
<div class="divfir"> <label>contact:</label><input type="text" id="stcontact" ><br /> <label>phone:</label><input type="text" id="stphone" ><br /> </div>
here get_ajax_csc.php code:
<?php if(isset($_post['stnum'])) { include("db.php"); $q=$_post["stnum"]; $sql="select contact,phone idc id='".$q."';"; $sel = $conn->query($sql); $arr = $sel->fetch(pdo::fetch_assoc); $tmparr = array( 'stcnt'=>$arr['contace'], 'stpho'=>$arr['phone'] ); echo json_encode($tmparr); } if(isset($_post['htmlcnt'])) { include("db.php"); $htcnt=stripslashes(".$_post['htmlcnt']."); ........ } ?>
here db.php code:
<?php session_start(); $pwd=$_session['password']; $user=$_session['user']; try { $conn = new pdo('mysql:host=x.x.x.x;port=3306;dbname=hpc',$user,$pwd); } catch (pdoexception $e) { echo "account or pwd wrong <meta http-equiv='refresh' content='1;url=index.html'>"; exit; } $conn->setattribute(pdo::attr_oracle_nulls, true); ?>
it seems nothing wrong in code, cann't fetch data database
i have found stripslashes() made ajax return nothing. when shielded method (//stripslashes()), worked fine. why stripslashes can influence ajax return data?
i found that:
$htcnt=stripslashes(".$_post['htmlcnt'].");
change to:
$htcnt=stripslashes($_post['htmlcnt']);
Comments
Post a Comment