View Full Version : Resolved Undefined Search [DOM]

01-12-2010, 11:48 PM
I fixed this by using json, instead of arrays.


This is kind of javascript and PHP - but the problem has to do with JS. ;)

I'm creating a suggest search, and a problem I'm facing is that I'm trying to get a variable form another file - and for some reason the javascript isn't gettnig it. =/

Here's my javascript/html:

<title>Suggest Search</title>
<script type="text/javascript">
var check, refresh;
var headEl = document.getElementsByTagName("head")[0];
var suggest = function(me){
// Getting Javascript //
if(check != true){
check = true;
} else {
refresh = document.createElement("script");
refresh.type = "text/javascript";
refresh.src = "info.php?search="+me.value;
headEl.insertBefore(refresh, document.getElementsByTagName('script')[0]);
// Displaying suggestions //
<input type="text" onkeydown="suggest(this);" onkeyup="suggest(this);" onchange="suggest(this);" onmouseover="suggest(this);" onclick="suggest(this);"/>

And here's my PHP/javascript:

header("Content-type: text/javascript");
if(!isset($_GET['search'])){ echo 'var search = new Array()'; }
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("general") or die(mysql_error());

$search = mysql_real_escape_string($_GET['search']);
$query = mysql_query("SELECT name FROM suggest WHERE name LIKE '%{$search}%'") or die(mysql_error());
$name = array();
while($row = mysql_fetch_assoc($query)){
$name[] = $row['name'];
$js_array = 'var search = new Array(';
foreach($name as $key => $value){
$js_array .= ($key == count($name)-1) ? "'$value'" : "'$value', ";
$js_array.= ');';
echo $js_array;

I'm getting this error:

If it is needed really badly - I'd be happy to post it online.

Thanks! Nile