Multiple Ajax Calls

How do i populate input field when i have multiple AJAX calls?

i have a drop down field dependant on the first drop down. I want to populate input fields with other row data from database dependant on the second selection.

The fist call works fine and populates a dependant dropdown after the first selection.
the second appears to be returning correct values but does not populate the input field.
I am very new to PHP.
I hope you can help :slight_smile:

e.g.

$(document).ready(function(){

    $("#selectWard").change(function(){

        var wardId = $(this).val();

        $.ajax({

              url:"ajaxpro.php",

              method:"POST",

              data:{'WardID':wardId},

               success:function(data){

                $("#patname").html(data);

                              }

        });

    });

    $("#patname").change(function(){

        var patId = $(this).val();

        $.ajax({

              url:"ajaxURN.php",

              method:"POST",

              data:{'PatID':patId},

               success:function(data){

                $("#URN").html(data);

                              }

        });

    });

});

PHP here (ajaxURN.php)

<?php

require('dbconfig.php');

$sql = "SELECT * FROM currentpatients WHERE ID ='".$_POST['PatID']. "'";

$result =mysqli_query($con,$sql);

while ($row=mysqli_fetch_array($result)){

$output  = '<input value ="'.$row["URN"].'">';

}
echo $output;
?>

input field mark up here: (index.php)

<div class = "row">
<label for="createURN">URN</label>
<input id = "URN" type="text"  name="createURN" value = "">
</div>

First, use prepared statements. You are using input from the user, that means it could be bad data.

You are looping through records, is there one urn for that id or several? Next, you are outputting the entire input element, don’t you just want the value? Select just the value in the query, then output it with json encoding. Then you parse the json object returned in the javascript and use the part you want.

Thanks for your reply!
The inputs are data driven selects so no unwanted user input in this case.

I was trying to display the value into an input field but using a label is now working. almost

There is only one URN per person ID but i’m reading from the entire row, once i get this one working I want to populate multiple fields with the other values in the row. I tried without a loop but did not manage to return a value at all.

however, noted regarding bad user input i will ensure to keep that in mind when using import fields in general

It’s still user input. How hard do you think it is to change html on a page?

If you make the switch to PDO, you can just fetchAll.

Sponsor our Newsletter | Privacy Policy | Terms of Service