简体   繁体   中英

Post value into textbox using ajax

I'm trying to post the value into textbox, depending on the value of my other textbox. Just for ie I input pencil in try1 field, the value should show in try2 field is A00003. I got it in alert message. But the problem is there's no value returning in textbox try2. What is the problem with this?

Any help will appreciate.

Table Structure

Item    |  Item_Code
Pencil  |  A00003  
Ballpen |  A00004

index.php

<input id="try1" name="try1">
<input id="try2" name="try2">
  <script type="text/javascript">
$(document).ready(function()
{
$('input[id="try1"]').change(function() 
{
var try1 = $("#try1").val();

$.ajax({
    type: "POST",
    url: "getajax.php",
data :"try1="+try1,
dataType:'html',
type:'POST',
   success:function(data){
   alert(data)
    $('#try2').html(data);
   } 
  });
return false;
});
});
</script>

GetAjax.php

<?php
$mysqli = new mysqli("localhost", "root", "", "2015");
if(isset($_POST['try1']))
{
$try1 = $_POST['try1'];
$sql = $mysqli->query("select item_code from code where item='$try1'");

while($row1 = $sql->fetch_assoc())
  {
  $code = $row1['item_code'];
  }
echo $code;
}
?>

使用val()作为输入元素。

$('#try2').val(data);

for writting a data into input box you need to use val() like this

$('#try2').val(data);

html() is generally used for writting html contents into divs ,spans , etc .

Additional Info : for writting data into a label you can use text("data")

数据分配给

$('#try2').value =data;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM