CodeIgniter Forums
Database LiveSearch - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Database LiveSearch (/thread-52125.html)

Pages: 1 2 3 4


Database LiveSearch - El Forum - 05-30-2012

[eluser]joe.afusco[/eluser]
Pretty new to PHP and CodeIgniter. Not sure that I am even doing this the best way possible. Attempting to live search a database, so when a user begins typing in a search field, the results populate in a div. Followed this video:

http://www.youtube.com/watch?v=mSAbp_4ACaw

Here is what I have so far. My search is calling the javascript function:

Code:
<input type="text" class="search-query" placeholder="Search" />

Although, it seems when I wrap code tags, it hides my onkeyup event. But it is there.

Javascript function:

Code:
function getSearchResults(value) {
   $.post("getSearchResults.php",{partialSearch:value}, function(data){
    $("#search_results".html(data));
   });
  }

And the php file that searches:

Code:
<?php

$partialSearch = $_POST['partialSearch'];

$results = $this->db->query("SELECT email_address FROM users WHERE email_address LIKE '%$partialSearch%'");

while ($resultsArray = mysql_fetch_array($results)) {
echo "<div>".$resultsArray['email_address']."</div>";
}


?&gt;

Can't seem to spot the error. Note, these are all 3 on separate files, not sure if that is the issue. Search is in navigation.php view, which is loaded on every page, the javascript is in the main user view which loads the navigation, and the php is a separate file all together, in the same directory as these views. Any help on this, or any better way of trying to accomplish the task would be much appreciated.




Database LiveSearch - El Forum - 05-30-2012

[eluser]sanir[/eluser]
hi,

can you put complete code of your view file.

Thanks,
Nasir Ranta


Database LiveSearch - El Forum - 05-30-2012

[eluser]joe.afusco[/eluser]
Main view file:

Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
[removed][removed]
[removed]
  function getSearchResults(value) {
   $.post("getSearchResults.php",{partialSearch:value}, function(data){
    $("#search_results".html(data));
   });
  }
[removed]
&lt;link href="/crm/css/bootstrap.css" rel="stylesheet"&gt;

&lt;title&gt;User Home&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<div id="everything_wrapper">
<div id="header_wrapper">
&lt;?php $this->load->view('includes/header'); ?&gt;
</div>
<div id="body_wrapper">
  &lt;?php ini_set('date.timezone', 'America/New_York'); ?&gt;  
  &lt;?php $this->load->view('includes/admin_navigation'); ?&gt;
  &lt;?php $this->load->view('includes/navigation'); ?&gt;
  &lt;?php $this->load->view('includes/body'); ?&gt;
</div>

<div id="footer_wrapper">
&lt;?php $this->load->view('includes/footer'); ?&gt;
<div id="search_results"></div>
</div>

</div>

&lt;/body&gt;
&lt;/html&gt;




Database LiveSearch - El Forum - 05-30-2012

[eluser]sanir[/eluser]
please confirm did u use jquery file in your view file

Thanks,
Nasir Ranta


Database LiveSearch - El Forum - 05-30-2012

[eluser]joe.afusco[/eluser]
Hmm. Keeps kicking errors when I try to post my navigation.php contents. The only relevant bit of that file should be the search box, which I previously posted.


Database LiveSearch - El Forum - 05-30-2012

[eluser]joe.afusco[/eluser]
[quote author="sanir" date="1338386410"]please confirm did u use jquery file in your view file

[/quote]

Yes, it is on there.


Database LiveSearch - El Forum - 05-30-2012

[eluser]sanir[/eluser]
Code:
$partialSearch = $_POST['partialSearch'];
$query = $this->db->select('email_address ')
->like('email_address', $partialSearch);
->get('users');
  
$result = $query->result();
$data = "";
foreach($result as $row){
      $data = $data . "<div>" . $row->email_address . "</div>";
}
echo $data;


try this code.


Database LiveSearch - El Forum - 05-30-2012

[eluser]joe.afusco[/eluser]
Still nothing being generated. I tested my onkeyup event with an alert, so I know it is at least making it that far. I have my database autoloading, so I assume it isn't a db connection issue. Not sure what else it could be.


Database LiveSearch - El Forum - 05-30-2012

[eluser]boltsabre[/eluser]
Use firebug (firefox plugin) and it's "console" tab, it will give you some error messages.

Do you have CSRF autoloaded? This can play havoc with jQuery/ajax requests.


Database LiveSearch - El Forum - 05-30-2012

[eluser]joe.afusco[/eluser]
It issues the following error:

POST http://localhost/crm/index.php/getSearchResults.php

404 Not Found

Is there a specific place I should put this file so it can find it? Not sure of the best way to follow the mvc pattern with this function.


No CSRF is not autoloading.