ajax 显示从 PHP 到输入文本的返回值


ajax display return value from php to a input text

大家好,我刚刚开始学习Ajax。

IM 尝试使用 Ajax 调用和显示到文本字段从 PHP 获取返回值。这是我的Ajax代码。

var getitem=$('#selectedItemId2').val();
        if (getitem==''){
        //do nothing
        }else{
            // verifying if item code is already been save and put to the text fields 
            $('#gettingitem').css('display','block');
            $('#gettingitem').html();
            $.ajax({
                    type: 'POST',
                    url:   'veritemcode.php',
                    datatype: 'text',
                    data:{'getitem':getitem
                    },
                    success:function(data){
                        window.setTimeout(function(){
                            $('#gettingitem').css('display','none');
                            $('#disp').css('display','block');
                            $('#disp').html(data);

                          });
                    }
            });}

这是我的VeritemCode.php我想显示在文本字段上

 $getitem=$_REQUEST['getitem'];
        $verifyitem=mysql_query("select * from item_master where item_code='".$getitem."'") or die (mysql_error());
        $vernum=mysql_num_rows($verifyitem);
        if($vernum!=1){

        }else{
                while($dispresult=mysql_fetch_array($verifyitem)){
                             echo $dispresult['item_desc'];
                             echo $dispresult['sup_item_code'];
                             echo $dispresult['smalluom'];
                }
        }

我想要的只是显示此字段的回显

<input type='text' id='itemdesc'/>
<input type='text' id='supitem'/>
<input type='text' id='smalluom'/>

请帮我解决这个问题...

我会执行以下操作...

用特殊字符(例如管道(拆分 PHP 中的 AJAX 数据:

            while($dispresult=mysql_fetch_array($verifyitem)){
                         echo $dispresult['item_desc'] . "|";
                         echo $dispresult['sup_item_code'] . "|";
                         echo $dispresult['smalluom'];
            }

然后在 html/JavaScript 中紧接 success:function(data({ 添加以下内容:

                    var split_data=data.split("|");
                    $("#itemdesc").val(split_data[0]);
                    $("#supitem").val(split_data[1]);
                    $("#smalluom").val(split_data[2]);
ok no need to worry about the code just put the following code in ur file veritemcode.php
<?php
$getitem=$_REQUEST['getitem'];
        $verifyitem=mysql_query("select * from item_master where item_code='".$getitem."'") or die (mysql_error());
        $vernum=mysql_num_rows($verifyitem);
        if($vernum!=1){

        }else{
                while($dispresult=mysql_fetch_array($verifyitem)){
                            $ites_descVal = $dispresult['item_desc'];
                             $supItemVal=$dispresult['sup_item_code'];
                             $smallomVal=$dispresult['smalluom'];
                }
        }
?>
<input type='text' id='itemdesc' value="<?=$ites_descVal?>"/>
<input type='text' id='supitem' value="<?=$supItemVal?>"/>
<input type='text' id='smalluom' value="<?=$smallomVal?>"/>

and remove 
<input type='text' id='itemdesc'/>
<input type='text' id='supitem'/>
<input type='text' id='smalluom'/>
from ur main page (php/html : whatever)