I have a selectAll function which returns the results as objects.
$customers = $app['database']->selectAll('customers');
Here is the var_dump for $customers variable just in case:
array(4) {
[0]=> object(stdClass)#5 (7) { ["id"]=> string(2) "10" ["name"]=> string(10) "Thisted El" ["address"]=> string(13) "Otto Monsteds" ["city"]=> string(5) "Vej 8" ["phone"]=> string(9) "503982350" ["zip"]=> string(6) "481922" ["notes"]=> string(0) "" }
[1]=> object(stdClass)#6 (7) { ["id"]=> string(2) "11" ["name"]=> string(11) "Bibin Vinod" ["address"]=> string(8) "Kottayam" ["city"]=> string(5) "Kochi" ["phone"]=> string(10) "0294294022" ["zip"]=> string(6) "129042" ["notes"]=> string(0) "" }
}
I need to use the 'name' property of these objects for an auto-fill form. I am using the autocomplete script in this link.
In my php file, I have added the above autofill function after the form. Then I use json_encode on this object followed by JSON.parse. I use a loop to add just the names to a javascript array, and finally I pass it to the autocomplete function.
var js_data = '<?php echo json_encode($customers); ?>';
var js_obj = JSON.parse(js_data);
for (var i = 0; i < arrayLength; i++)
{
customername[i] = js_obj["name"];
}
autocomplete(document.getElementById("customers"), customername);
However the autofill form is not working. I'd like help regarding this issue. Thanks.
var js_obj = <?php echo json_encode($customers) ?>;<form autocomplete="off" method = "POST" action = "database/NewProduct.php"> <div class="form-row"> <div class="autocomplete form-group col-md-3"> <label>Customer</label> <input id="customers" class ="form-control" type="text" name="customers" id="customers" placeholder="Search.."> </div></div>customername is a null array when I debug.