1

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.

1
  • you are not returning anything from your locationAjax.php? so you can either save the html in string and return it or return the results from your query. Commented Aug 20, 2015 at 14:45

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

9 Comments

insert $row = json_encode(mysqli_fetch_assoc($result)); return $row; in your locationajax.php file right after result = .... ;
yes i have done that if ($result = mysqli_query($conn, $sql)) { $row = json_encode(mysqli_fetch_assoc($result)); return $row; }
i also have: success: function(data){ if(!data) { alert('empty'); return; } but it gives me the alert that data = empty: how to check my php array if it got results then
point to your file locationajax.php from your url and vardump($result); what do you get?
Notice: Undefined index: id in C:**\locationAjax.php on line 4 --> $locationID = $_POST['id'];
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.