Jump to content

show value in the texbox using ajax


eloginko

Recommended Posts

I have a table called tb_app with fields 'id','aic','batchcode','name'

 

example table value: '1','0001','1','james'.

 

As you can see there are three textboxes(name)(aic)(batchode).

 

I want to show the value of aic and batchcode in their correspoding textboxes after typing the specific name in the textbox(name).

The problem is when you type james in the textbox(name) it shows '00011' in the (aic)textbox and (batchcode)textbox...i want it to be '0001' in aic and '1' in the batchcode if i type james which is stored value in the table.

 

 

 

html code:

 

    <form method="post">

    <input type="text" name="names" id="query" onblur="getvalues()"/>

    <input type="text" name="aic" id="aic"/>

    <input type="text" name="batchcode" id="batchcode" />

    </form>

 

script code:

 

 

    <script type="text/javascript">

     function getvalues()

        {

    

             var selname = $("input[name=names]:text").val(); 

    

                $.ajax({ url: "getuserdata.php",

                    data: {"selname":selname},

                    type: 'post',

                    success: function(output) {

                                    $("#aic").val(output);

                                    $("#batchcode").val(output);

    

                    }

    

                });

        }

    </script>

 

 

getuserdata.php page:

 

    <?php 

    include('include/connect.php');

    $selname = $_POST['selname'];

    $query = "SELECT * FROM tb_app WHERE tb_app.name RLIKE '$selname'";

    $result = mysql_query($query) or die(mysql_error());

    while($rows = mysql_fetch_array($result)){

    echo $rows['aic']; 

    echo $rows['batchcode'];

    }

    ?>

Link to comment
Share on other sites

Echo do not send separate responses. You are setting aic and batchcode to same value as the response (00011). What you should do is json_encode the response

    while($rows = mysql_fetch_array($result)){
        $response = array($rows['aic'], $rows['batchcode']); // add return data to an array
        echo json_encode($response); // json encode that array
    }

Now set each text field the corresponding value from json array

                    dataType: "json",                               # the datatype to expect from the response
                    success: function(data) {
                                    $("#aic").val(data[0]);       #  first item is aic value
                                    $("#batchcode").val(data[1);  # second item is batchcode value
    
                    }
Link to comment
Share on other sites

I have tested the code and it appears to work fine.

 

EDIT: Remove the # comments from the code before you try.

 

My test code

<?php

if(isset($_POST['selname']))
{
    $response = array('0001', '1'); // add return data to an array
    echo json_encode($response); // json encode that array

    exit;
}

?>

<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        function getvalues()
        {
             var selname = $("input[name='names']:text").val(); 
    
                $.ajax({ url: "",
                    data: {"selname":selname},
                    type: 'post',
                    dataType: "json",
                    success: function(output) {
                      console.log(output);
                                    $("#aic").val(output[0]);
                                    $("#batchcode").val(output[1]);
    
                    }
    
                });
        }
    </script>
    </script>
  </head>
  <body>
    <form method="post">
      <input type="text" name="names" id="query" onblur="getvalues()"/>
      <input type="text" name="aic" id="aic"/>
      <input type="text" name="batchcode" id="batchcode" />
    </form>
  </body>
</html>
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.