-1

I have this PHP code to select data from a database:

<?php
include './database_connect.php';

$ppid=$_POST['selectPatientID'];

$query="SELECT * FROM patient WHERE p_Id='$ppid'";

$result=  mysqli_query($conn, $query);
while($row=  mysqli_fetch_array($result)){
    echo ($row['p_fname']); 
    echo ' ';
    echo ($row['p_lname']);
}    
?>  

But I need to get the p_fname and p_lname values separately. Is there anything I could do to achieve this?

This is my jQuery:

    <script>
                $(document).ready(function () {
                    $('#selectPatientID').on('change', function (event) {
                        event.preventDefault();
//                        alert("Hi");
                        $.ajax({
                            url: "UDPatient.php",
                            type: 'POST',
                            data: {selectPatientID: $('#selectPatientID').val()},
                            dataType: 'html',
                            success: function (result) {
                                $('#patientName').val(result); //in here, how can I get the p_fname and p_lname separetely and put them in two different input fields?
                            }
                        });
                    });
                });
            </script>
Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
Jananath Banuka
  • 663
  • 3
  • 8
  • 18

2 Answers2

0

you have to json_encode

$row =  mysqli_fetch_array($result)){

header('Content-Type: application/json');// header for json encode response
echo json_encode($row);
exit;

Your jQuery ajax code will be:

$.ajax({
   url: "UDPatient.php",
   type: 'POST',
   data: {selectPatientID: $('#selectPatientID').val()},
   dataType: 'json',// to receive data into json format
   success: function (result) {
       alert(result.p_fname);
       alert(result. p_lname);
   }
}); 
usman ikram
  • 461
  • 4
  • 10
0

You should try something along the lines of

<?php
include './database_connect.php';

$ppid=$_POST['selectPatientID'];

$query="SELECT * FROM patient WHERE p_Id='$ppid'";

$result=  mysqli_query($conn, $query);


$row=  mysqli_fetch_array($result);
echo json_encode($row);
?>  

You add the query results into an array $row, which you json_encode() and echo as the result

<script>
    $(document).ready(function () {
        $('#selectPatientID').on('change', function (event) {
            event.preventDefault();
            $.ajax({
                url: "UDPatient.php",
                type: 'POST',
                data: {selectPatientID: $('#selectPatientID').val()},
                dataType: 'html',
                success: function (result) {
                    var jsonResult = JSON.parse(result);
                    var p_fname = jsonResult.p_fname;
                    var p_lname = jsonResult.p_lname;
                    // whatever else
                }
            });
        });
    });
</script>

The json result can be parsed ( or the dataType set as'json' alternatively').

Manav
  • 1,357
  • 10
  • 17