notobits
notobits

Reputation: 25

ajax display return value from php to a input text

hi everyone im just started learning ajax.

im trying to get the return value from php using ajax call and display to text fields. here is my ajax code.

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);



                          });
                    }
            });}

and this is my veritemcode.php that i want to display on the text fields

 $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'];

                }

        }

all i want is to display the echo to this fields

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

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

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

Please help me with this problem...

Upvotes: 0

Views: 2648

Answers (2)

MilMike
MilMike

Reputation: 12851

I would do the following...

split the ajax data in php by a special character, for example a pipe:

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

then in html/javascript right after success:function(data){ add the following:

                    var split_data=data.split("|");
                    $("#itemdesc").val(split_data[0]);
                    $("#supitem").val(split_data[1]);
                    $("#smalluom").val(split_data[2]);

Upvotes: 3

vivek salve
vivek salve

Reputation: 991

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)

Upvotes: 1

Related Questions