Nuri Ensing
Nuri Ensing

Reputation: 2030

auto fill input text boxes using AJAX and PHP and Javascript

I have a selectbox with options generated with data from a database(phpmyadmin) the database looks like this:

Columns: locationID - address - postalcode - place

After the select box I have some input fields, at first standard with some placeholders.

The idea is that if I select an option that the fields will be generated and filled from the database (except the contactperson field, this will stay manual)..

For this I try to use AJAX.

I have already made this:

test.php:

<?php
    //Query the database for the results we want
    $query1 = $conn->query("SELECT * FROM deliveryaddress");

    //Create an array of objects for each returned row
    while($locationArray[] = $query1->fetch_object());

    //Remove the blank entry at end of array
    array_pop($locationArray);
?>

<script>
    $(document).on("change","#select-box").function(){
        var id = $(this).val();
        $.ajax({
            url      : "locationAjax.php",
            data     : {"id":id},
            type     : "POST",
            dataType : "HTML",
            success  : function(data){
                // here you can write code to asign the values to text boxes.
                $(".wrapperDiv").html(data); 
            }
        });
    });
</script>

<div class="wrapperDiv">
    <label for="locationLabel">Locatie</label>
    <select name="locations" id="locationSelectBox" >
        <option>Locatie</option>
        <?php foreach ($locationArray as $option) : ?>
            <option value="<?php echo $option->locationID; ?>"><?php echo $option->locationID; ?></option>
        <?php endforeach; ?>
    </select>

    <label for="address" style="float:left;">Straat/No</label>
    <input type="text" name="address" id="address" placeholder="Straatnaam en nummer" />

    <label for="postalCode">Postcode</label>
    <input type="text" name="postalCode" id="postalCode" placeholder="Postcode" />

    <label for="place">Plaats</label>
    <input type="text" name="place" id="place" placeholder="Plaats" />

    <label for="contactPerson">Contact</label>
    <input type="text" name="contactPerson" id="contactPerson" placeholder="Contactpersoon" />
</div>

locationAjax.php:

<?php
    require_once('dbconnection.php'); 
    $locationID = $_POST['id']; 
    $sql = "SELECT * FROM deliveryaddress where id = $locationID"; 
    $result = mysqli_query($conn, $sql); 
?> 
<?php while ($row = mysqli_fetch_assoc($result)) { ?> 
    <input type="text" name="name" value="<?= $row['address'] ?>" /> 
    <input type="text" name="name" value="<?= $row['postalcode'] ?>" /> 
    <input type="text" name="name" value="<?= $row['place'] ?>" /> 
} 
?>

unfortunatly, it doesn't work.

Upvotes: 1

Views: 7013

Answers (2)

mdamia
mdamia

Reputation: 4557

try this

echo json_encode(mysqli_fetch_assoc($result));

in your script loop, console log data to view the result for troubleshooting
change your script to

$(document).on("change", "#locationSelectBox").function() {
  var id = $(this).val();
  $.ajax({
    url : "locationAjax.php",
    data : {
     "id" : id
    },
    type : "POST",
    dataType : "json",
    success : function(data) {
      console.log(data);
      //
      for (var x in data) {
        $('.address').val(data.address);
        $('.postalcode').val(data.postalcode);
        $('.place').val(data.place);
      }
    }
  });
});

check your console log for errors

Upvotes: 1

meksof
meksof

Reputation: 21

Instead of this line: $(document).on("change","#select-box").function(){

Try this: $(document).on("change","#locationSelectBox").function(){

Then console.log the data on the ajax "success" function And tell me what you get

Upvotes: 1

Related Questions