简体   繁体   English

我可以使用function(data)jquery提取并填充不同的字段吗?

[英]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 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 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 尝试从数据库中提取名称和地址并对其进行json

$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 之后,通过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: JavaScript代码:

<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代码:

<?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']); 
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jquery创建表-完成第一个功能后,我是否可以自动填充字段? - Creating a table using jquery - can I then populate a field automatically once the first function is done? 如何使用jQuery填充输入字段 - How to populate input fields using jQuery 如何使用php和mysql在单行引导滑块中获取不同的数据结果 - how can i fetch different data results in single row of bootstrap slider using php and mysql 为什么我不能通过使用curl_setopt和Ajax jquery来获取数据? - Why I can't fetch data on the sometime by using curl_setopt and Ajax jquery? 如何在不使用隐藏字段值单击按钮的情况下从jQuery中的MySQL中获取数据? - How can I fetch data from MySQL in jQuery without a click of a button using an hidden field value? 我正在使用来自sql中两个不同表的两个sql字段来填充选择框 - I'm using two sql fields from two different tables in sql to populate a select box 如何在不同的表列中获取另一个表数据? - How can i fetch another table data in different table column? 使用AJAX从php文件中获取多个数据并将其插入不同的输入字段中 - Fetch multiple data from a php file using AJAX and insert them in different input fields 如何从daabase使用jquery ajax获取html表单字段的数据 - How to fetch data of html form fields using jquery ajax from daabase 使用jQuery使用数据库中的数据填充下拉列表 - Populate dropdown with data from database using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM