12 Replies - 364 Views - Last Post: 02 February 2012 - 11:02 AM Rate Topic: -----

Topic Sponsor:

#1 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 13-December 10

Populating Autocomplete List From Database

Posted 02 February 2012 - 06:53 AM

Hello Guys,
I cannot seem to populate the autocomplete list of the Product Names that I am fetching through AJAX from my DATABASE. For some reason, the list shows only "Array" and not the actual name of the products. There is no problem with the SQL. This is my code -- Help would be appreciated. Thanks in advance.

PHP File:
<?php require_once("BusinessLogic/ProductCRUD.php"); 

$productsCrud = new ProductCRUD();
$productNames = $productsCrud->GetProductNames($_POST["products"]);

?>

<html>
<head>
	<script type="text/javascript" language="javascript" src="ajax.js"></script>
    
</head>
    <body>
    <form>
        <input type="text" id="products" placeholder="Product Name " style="width:200px" list="productsList" onkeydown="javascript:ajax_post()"/>
        <datalist id="productsList">
        <select name="prodList">
         		<?php
		            foreach ($productNames as $pN)
			    {
               			echo "<option value='" . $pN . "'>" .  $pN . "</option>";
            	}
         		?>
      			</select>	
        </datalist>
    </form>
    </body>
</html>
    
    




AJAX Javascript:
function ajax_post(){
        var hr = new XMLHttpRequest();
        var url = "html5test.php";
        var pl = document.getElementById("products").value;
        var vars = "products="+pl;
        
        hr.open("POST", url, true);
		hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.send(vars);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Populating Autocomplete List From Database

#2 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 118
  • View blog
  • Posts: 443
  • Joined: 20-December 08

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 07:49 AM

I assume $pN is an array - try $pN['productName'] on line 21.

Obviously change productName to the correct value.

It might help if you do.

echo "<pre>";
print_r($pN);
echo "</pre>";


This post has been edited by KingCuddles: 02 February 2012 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1940
  • View blog
  • Posts: 7,294
  • Joined: 08-August 08

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 07:51 AM

So $productNames is an array of arrays? Try outputting it to a file, error_logging it, or echoing it to see what it consists of:

print_r($productNames, true);


Was This Post Helpful? 0
  • +
  • -

#4 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 13-December 10

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 08:20 AM

It doesn't display anything. $productNames is an array but it is a simple array, not dimensional or associative. It consists of a single dimensional array, and $pN I have used to fetch every item from it. I can't seem to figure out where the problem could be.
Just for the sake of reference here is the SQL:
public function GetProductNames($input)
	{
		$db = $this->GetMysqlConnection();
        if($input == "")
		{
			$query = "SELECT product_name from product";

		}
		else
		{
		        $query = "SELECT product_name from product
                  WHERE product_name like '%$input%'";			
		}
		
        $dsProduct = $db->Execute($query);
		return $dsProduct;
	}
	   

This post has been edited by huzaifa.iu: 02 February 2012 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 118
  • View blog
  • Posts: 443
  • Joined: 20-December 08

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 08:22 AM

Can you post the result of print_r($productNames)?
Was This Post Helpful? 0
  • +
  • -

#6 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 13-December 10

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 08:36 AM

View PostKingCuddles, on 02 February 2012 - 08:22 PM, said:

Can you post the result of print_r($productNames)?

It doesn't display anything through that statement. But in the resulting input type autocomplete list, it displays a list of "Array" items. But not the actual item names from the database.
Was This Post Helpful? 0
  • +
  • -

#7 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 118
  • View blog
  • Posts: 443
  • Joined: 20-December 08

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 08:41 AM

Hmm print print_r($productNames) shows nothing? Surly it should show the results from the database as an array.

I don't see any check to see if $productNames is actually not just an empty result?

Also try:

foreach ($productNames as $pN) {
  echo "<option value='" . $pN . "'>" .  $pN . "</option>";
  var_dump($pN);
}


Was This Post Helpful? 0
  • +
  • -

#8 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 13-December 10

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 09:02 AM

View PostKingCuddles, on 02 February 2012 - 08:41 PM, said:

Hmm print print_r($productNames) shows nothing? Surly it should show the results from the database as an array.

I don't see any check to see if $productNames is actually not just an empty result?

Also try:

foreach ($productNames as $pN) {
  echo "<option value='" . $pN . "'>" .  $pN . "</option>";
  var_dump($pN);
}



It still didn't show anything. However, I did this change in the function but now I only get the first entry from the database. Other entries are all blank.
$dsProduct = $db->Execute($query);
		 $product = array();
		 $i=0;	        

        while($rProduct = $dsProduct ->FetchRow())
        {
        $product[$i] = $rProduct[$i];
		$i++;			
        }
        
        return $product;


Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1940
  • View blog
  • Posts: 7,294
  • Joined: 08-August 08

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 09:10 AM

I think you're suffering from the confusion created by mixing HTML and PHP. You need to understand that they're processed on different machines at different times. Mixing them doesn't change that fact.

When your page is first displayed there is no post value, so PHP calls:
$productsCrud->GetProductNames("");


Was This Post Helpful? 1
  • +
  • -

#10 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 13-December 10

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 09:43 AM

View PostCTphpnwb, on 02 February 2012 - 09:10 PM, said:

I think you're suffering from the confusion created by mixing HTML and PHP. You need to understand that they're processed on different machines at different times. Mixing them doesn't change that fact.

When your page is first displayed there is no post value, so PHP calls:
$productsCrud->GetProductNames("");


Oh alright, that gets rid of the "products" index error, but then how do I get the ajax to send the data to the file AFTER the event has been triggered? Cause once the page is loaded, it runs all the script inside I suppose. Please look at the ajax part of my code, do you find any problem in there? I am still learning AJAX so I am not that good at that yet. So if you could guide me it would be great.

This post has been edited by huzaifa.iu: 02 February 2012 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1940
  • View blog
  • Posts: 7,294
  • Joined: 08-August 08

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 10:03 AM

The way I do Ajax is that I look at the final HTML, not what PHP sees, but what it actually sends to the browser. Then I write my Javascript to send its requests based on that. This is yet another reason why I believe PHP and HTML should be separated at all times.

When you've got your Javascript portion written then you know what the PHP side needs to return for values, so you write that script accordingly. It's all very simple unless you mix languages.
Was This Post Helpful? 0
  • +
  • -

#12 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 13-December 10

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 10:42 AM

OK thanks. I got the codes in separate files, and now I even get the print_r($productNames) result as this:
Array ( [0] => Array ( [0] => Dell Inspiron [product_name] => Dell Inspiron ) [1] => Array ( [0] => SanDisk 8GB [product_name] => SanDisk 8GB ) [2] => Array ( [0] => SanDisk 4GB [product_name] => SanDisk 4GB ) )
But, the option list is still not getting populated, and shows the same "Array" item instead of the actual name of the item.
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1940
  • View blog
  • Posts: 7,294
  • Joined: 08-August 08

Re: Populating Autocomplete List From Database

Posted 02 February 2012 - 11:02 AM

So $productNames is a 2D array. You probably could do something like:
                    foreach ($productNames[0] as $pN)


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1