html - How can we fetch data from database in PHP using ajax and display in value of input type?

one text

Solution:

First, let's change ClientData.php to return data in a more usable form.

if(isset($_POST["action"]) && $_POST["action"]=="retreive"){

    $insertData = $_POST['insertdata'];

    //select first row of results for getSlideForEdit by adding `[0]`
    $slideOne = $objMaster->getSlideForEdit($insertData)[0];
    
    //notice array keys match form1 form elements ID
    //notice there is no need for a loop here
    echo json_encode(
        array(
            'title' => $slideOne['title'], 
            'description' => $slideOne['description']
        )
    );

    //if you want to update EVERYTHING from $slideOne, you can just do
    //echo json_encode($slideOne);
    //instead of the above json_encode()

}

Now our return will contain JSON data instead of plain strings. We can update your success method to update those input boxes.

...
data: formData,

//set dataType to json because we are receiving json from the server
dataType: 'json',
success:function(msg){
    $('#step1').addClass('in active');
    
    //if you don't set dataType to json you can also do
    //msg = JSON.parse(msg);

    //loop through returned array.
    $.each(msg, function(index, value) {
        
        //select element by index e.g `#title`, set value
        $('#' + index).val(value);
    });

    alert('success');                     
}

This solution will dynamically update any input on your page as long as you return a key=>value pair for it from your server.

Source