How to fetch data from an ajax calling page and set it to a textbox?

Here I want to get data from a page called with an ajax call and set it as a textbox. I'm not that perfect with ajax, but I've tried like this:

<tr>
<td style="text-align:left"> <label for="pat_id" >Patient ID</label></td>
<td><input type="text" name="patient_id" id="patient_id"  readonly></td>
</tr>

      

Ajax call code:

$(document).ready(function()
{
    $.ajax({
    type: "GET",
    url:"retriveID.php",
    data: ({type: 'patient'}),
    success:function(html){ 
        $("#patient_id").html(html);        
    }
    });  
});

      

And retriveID.php:

<?php
include("db.php");
$type = $_GET['type'];
if($type=='patient')
{
     $retrive_id=mysql_query("select patient_id from patient order by        patient_id desc limit 1") or die (mysql_error());
     $row=mysql_fetch_array($retrive_id);
     $patient_id="PAT".$row['0']+1;
}   

?>

      

I know this is a wrong call or data request.

+3


source to share


3 answers


You don't have an answer to your result.

<?php
include("db.php");
$type = $_GET['type'];
if($type=='patient')
{
     $retrive_id=mysql_query("select patient_id from patient order by        patient_id desc limit 1") or die (mysql_error());
     $row=mysql_fetch_array($retrive_id);
     $patient_id="PAT".$row['0']+1;
     echo  $patient_id;
}   
?>

      



and use

$("#patient_id").val(html);

      

+4


source


You need to get the echo value

echo $patient_id;

      



and then .html()

should be used instead .val(value)

because it patient_id

is an input element.

$("#patient_id").val(html);

      

0


source


Replace $("#patient_id").html(html);

with$("#patient_id").val(html);

0


source







All Articles