CodeIgniter Forums

Full Version: Database LiveSearch
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4

El Forum

[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.

El Forum

[eluser]sanir[/eluser]
hi,

can you put complete code of your view file.

Thanks,
Nasir Ranta

El Forum

[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;

El Forum

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

Thanks,
Nasir Ranta

El Forum

[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.

El Forum

[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.

El Forum

[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.

El Forum

[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.

El Forum

[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.

El Forum

[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.
Pages: 1 2 3 4