Linking autocomplete to mysql
Hey guys, I'm having a HUGE amount of trouble getting autocomplete linked to the mysql database I've got set up. I'm very new to javascript and php coding, so the trouble I'm seeing was expected but I never thought it'd give me this much trouble.
The database has one table that has Customer Names, Billing Addresses and Shipping Addresses. The end goal here is to have Autocomplete kick in for the Customer name, then narrow down the results possible for Billing Address and Shipping address.
What I'd like some help with though, is figuring out where I'm going wrong. I think my javascript is OK, and the php might be where the problem's coming from but I just can't be sure.
So, the problem with the code at this point is that when I begin typing it does not retrieve the list of customer names when 2 letters or more are typed into the field.
the javascript:
- $(function() {
- $("#CustomerName").autocomplete({
- source: "searchcustomer.php",
- minLength: 2,
- select: function(event, ui) {
- $('#customername').val(ui.item.id);
- }
- });
- });
The PHP
- <?php
- $con=mysqli_connect("localhost","root","","customer_info");
- if (mysqli_connect_errno())
- {
- echo "Failed to connect to MySQL: " .mysqli_connect_error();
- }
- $return_arr = array();
-
- if ($con)
- {
- $fetch = mysql_query("SELECT * FROM customer '%" . mysql_real_escape_string($_GET['term']) . "%'");
-
- /* Retrieve and store in array the results of the query.*/
- while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
- $row_array['label'] = $row['idcustomername'];
- $row_array['value'] = $row['customer']
- array_push($return_arr,$row_array);
- }
- }
- mysql_close($con);
- echo json_encode($return_arr);
- ?>
I think that's all the pertinent info. Any help you guys could offer would be greatly appreciated.