i got a little problem with searching my database and outputting the data.
This is my template. Don’t mind the $page->_(). this is because of my framework. just think of phps echo
<?php
$movies = array();
if( isset($_POST['searchtext']) && isset($_POST['ajax']) && $_POST['ajax']==1 )
$movies = Movie::getByTitle( $_POST['searchtext'] );
$list = '<ul id="results">';
if( isset( $movies ) && $movies!=null )
foreach($movies as $movie)
$list.= '<li>'.$movie->getTitle().'</li>';
else if( isset($_POST['ajax']) && $_POST['ajax']==1 )
$list.='<li>No Results!</li>';
$list.= '</ul>';
if(isset($_POST['ajax']) && $_POST['ajax']==1){
$json = array(
'success' => true,
'html' => $list
);
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Expires: 0');
header('Content-type: text/json');
echo json_encode($json);
die();
}
$page->_(
'<section class="dialog-fixed small">',
'<form id="search-form" method="post" action="#home">',
'<fieldset>',
'<input type="text" name="searchtext" />',
'<input type="submit" name="moviesubmit" value="Search"/>',
'</fieldset>',
'</form>'
);
$page->_($list);
$page->_('</section>');
?>
Movie::getByTitle just does a SELECT * FROM movies WHERE title LIKE '%".$title."%' LIMIT 30" query, creates for each result an Movie Object and return an array with the objects
My jQuery AJAX Request works like this:
var f0 = $('#search-form');
if( f0.length>0 ) {
f0.each(function(){
var f = $(this);
$(this).submit(function(ev){
ev.preventDefault();
$.ajax({
type: 'post',
url: BASEURL+f.attr('action'),
data: f.serialize()+'&ajax=1',
dataType: 'json',
success: function(data) {
if(data.success){
$('#results').replaceWith(data.html);
}
},
error: function(xhr,txt,err) {
alert(txt+' ('+err+')');
}
});
});
});
}
Now..the Database table i query has about 1.000.000 rows (movies)
It works very well if i search for “Forrest Gump” for example…But if i search for something with fewer Characters lets say “One” it doesnt return anything. I tried searching my Database inside of phpmyadmin for “One” and it returned about 15.000 results…okay i thought this could be too many for my script to handle so i put a LIMIT 30 at the end of my database query as you can see..
doesnt work either. i dont get anything in return after searching for “One” not even a “No Result” as it should (actually it shouldnt but you know what i mean). My script just stops working.
And if i searched for something like “One” i can’t search anything after this. Not even “Forrest Gump” gives me any results.
Really cant figure out where the problem is. My Firebug doesnt help me 😉
Any hints?
EDIT:
Ok i found out where my problem was. json_encode($json) didn’t work. i didnt figure out the reason yet. This solution works:
script.js:
$.ajax({
type: 'post',
url: BASEURL+f.attr('action'),
data: f.serialize()+'&ajax=1',
dataType: 'text',
success: function(data) {
if(data!=''){
$('#results').replaceWith(data);
}
},
error: function(xhr,txt,err) {
alert(txt+' ('+err+')');
}
});
php template:
if(isset($_POST['ajax']) && $_POST['ajax']==1){
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Expires: 0');
header('Content-type: text/html');
echo utf8_encode($list);
die();
}
Ok..found the problem. Some of the Strings in my database weren’t correctly UTF-8…json_encode() had trouble with this so in my php template i had to change
$list.= '<li>'.$movie->getTitle().'</li>';to$list.= '<li>'.utf8_encode($movie->getTitle()).'</li>';