简体   繁体   中英

can i fetch and populate different fields using function(data) jquery?

i want to populate two fields when some one fill up card no.

 <input type="text" id="name" name="name" class="form-control" Value="<?php echo $grow['name']; ?>">
 <input type="text" id="address" name="address" class="form-control" Value="<?php echo $grow['address']; ?>">

but this code populate field one by one. can any one suggest be better code for populating two fields from database. Thank you

jquery

<script type="text/javascript">
$(document).ready(function()
{
$("#krishi").keyup(function()
{
var k=$(this).val();
var q="name";


$.ajax
({
type: "POST",
url: "getresult.php",
data: 'k='+k+'&q='+q,
cache: false,
success: function(data)
{
    if(data){
    $("#name").val(data);
$.ajax
({
type: "POST",
url: "getresult.php",
data: 'k='+k+'&q=address',
cache: false,
success: function(data)
{
    if(data){
    $("#address").val(data);

    }

    } 
});
    }else
        $("#name").val("");
    $("#address").val("");
    } 
});

});



});
</script>

getresult.php

<?php

define('INCLUDE_CHECK',true);
include("mysql.php");


$k=$_POST['k'];
$q=$_POST['q'];
$sql=mysql_query("select * from inward where krishi='$k'");

$row=mysql_fetch_array($sql);
echo $row[$q]; 


?>

Try to extract both name and address from database and json them

$k=$_POST['k'];
$q=$_POST['q'];
$sql=mysql_query("select address,name from inward where krishi='$k'");

$row=mysql_fetch_array($sql);

$result = array(
               'name'=>$row['name'],
               'address'=>$row['address']);
echo json_encode($result);

After that parse them via jquery

$.ajax
({
type: "POST",
url: "getresult.php",
data: 'k='+k+'&q=address',
cache: false,
success: function(data)
{
    if(data){
    var parsedData = jQuery.parseJSON(data);
    $("#name").val(parsedData.name);
    $("#address").val(parsedData.address);

    }

    } 
});

Javascript Code:

<script type="text/javascript">
$(document).ready(function()
{
    $("#krishi").keyup(function()
    {
    var k = $(this).val();
    var q = "name";

    $.ajax({
        type: 'POST',
        url: "getresult.php",
        data: 'k='+k,
        cache: false,
        success: function(data)
        {
            var jsonArr = $.parseJSON(data);
            if(typeof response =='object')
            {
                $("#name").val(jsonArr.name);
                $("#address").val(jsonArr.address); 
            }
            else
            {
                $("#name").val("");
                $("#address").val("");  
            }
        }
    });
    });
});
</script>

PHP Code:

<?php
define('INCLUDE_CHECK',true);
include("mysql.php");

$k   = $_POST['k'];
$sql = mysql_query("select * from inward where krishi='$k'");

$row = mysql_fetch_assoc($sql);
echo json_encode(array('name' => $row['name'], 'address' => $row['address']); 
?>

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