eloginko Posted January 24, 2014 Share Posted January 24, 2014 I have a table called tb_app with fields 'id','aic','batchcode','name' example table value: '1','0001','1','james'. As you can see there are three textboxes(name)(aic)(batchode). I want to show the value of aic and batchcode in their correspoding textboxes after typing the specific name in the textbox(name). The problem is when you type james in the textbox(name) it shows '00011' in the (aic)textbox and (batchcode)textbox...i want it to be '0001' in aic and '1' in the batchcode if i type james which is stored value in the table. html code: <form method="post"> <input type="text" name="names" id="query" onblur="getvalues()"/> <input type="text" name="aic" id="aic"/> <input type="text" name="batchcode" id="batchcode" /> </form> script code: <script type="text/javascript"> function getvalues() { var selname = $("input[name=names]:text").val(); $.ajax({ url: "getuserdata.php", data: {"selname":selname}, type: 'post', success: function(output) { $("#aic").val(output); $("#batchcode").val(output); } }); } </script> getuserdata.php page: <?php include('include/connect.php'); $selname = $_POST['selname']; $query = "SELECT * FROM tb_app WHERE tb_app.name RLIKE '$selname'"; $result = mysql_query($query) or die(mysql_error()); while($rows = mysql_fetch_array($result)){ echo $rows['aic']; echo $rows['batchcode']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285639-show-value-in-the-texbox-using-ajax/ Share on other sites More sharing options...
Ch0cu3r Posted January 24, 2014 Share Posted January 24, 2014 (edited) Echo do not send separate responses. You are setting aic and batchcode to same value as the response (00011). What you should do is json_encode the response while($rows = mysql_fetch_array($result)){ $response = array($rows['aic'], $rows['batchcode']); // add return data to an array echo json_encode($response); // json encode that array } Now set each text field the corresponding value from json array dataType: "json", # the datatype to expect from the response success: function(data) { $("#aic").val(data[0]); # first item is aic value $("#batchcode").val(data[1); # second item is batchcode value } Edited January 24, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285639-show-value-in-the-texbox-using-ajax/#findComment-1466389 Share on other sites More sharing options...
eloginko Posted January 24, 2014 Author Share Posted January 24, 2014 @Ch0cu3r the code you present doesn't work?? did you try it? Quote Link to comment https://forums.phpfreaks.com/topic/285639-show-value-in-the-texbox-using-ajax/#findComment-1466393 Share on other sites More sharing options...
Solution Ch0cu3r Posted January 24, 2014 Solution Share Posted January 24, 2014 (edited) I have tested the code and it appears to work fine. EDIT: Remove the # comments from the code before you try. My test code <?php if(isset($_POST['selname'])) { $response = array('0001', '1'); // add return data to an array echo json_encode($response); // json encode that array exit; } ?> <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> function getvalues() { var selname = $("input[name='names']:text").val(); $.ajax({ url: "", data: {"selname":selname}, type: 'post', dataType: "json", success: function(output) { console.log(output); $("#aic").val(output[0]); $("#batchcode").val(output[1]); } }); } </script> </script> </head> <body> <form method="post"> <input type="text" name="names" id="query" onblur="getvalues()"/> <input type="text" name="aic" id="aic"/> <input type="text" name="batchcode" id="batchcode" /> </form> </body> </html> Edited January 24, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285639-show-value-in-the-texbox-using-ajax/#findComment-1466400 Share on other sites More sharing options...
eloginko Posted January 24, 2014 Author Share Posted January 24, 2014 @Ch0cu3r it only show 0001,1 in the aic and batchcode textbox even its not? i try to change the value in the database but it shows '0001' and '1' all the time? Quote Link to comment https://forums.phpfreaks.com/topic/285639-show-value-in-the-texbox-using-ajax/#findComment-1466410 Share on other sites More sharing options...
eloginko Posted January 24, 2014 Author Share Posted January 24, 2014 @Ch0cu3r i forgot to put the url it worked now thx Quote Link to comment https://forums.phpfreaks.com/topic/285639-show-value-in-the-texbox-using-ajax/#findComment-1466412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.