/    Sign up×
Community /Pin to ProfileBookmark

multiple live search AJAX not calling file

Hi,

I am trying to amend the showHint Function from w3schools.

I adapted the basic script to allow multiple calls and I don’t have a permanent array so I made my php page create one from the table.

Have tried to play around with this a bit but I currently get nothing.

My script is:

[CODE]
<head>
<script type=”text/javascript”>
function showHint(str,FILE+str,ID){
if (str.length==0){
document.getElementById(ID).innerHTML=””;
return;
}
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}

xmlhttp.open(“GET”,FILE,true);
xmlhttp.onreadystatechange=function(){
if {xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(ID).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send()
}
</script>
</head>
<body>
<label>Search by name: </label>
<input type=”text” onkeyup=”showHint(‘getNames.php?q=’,’txtHintName’)” Value=”Search” />
<br />
<div id=”txtHintName”></div>
<br />
</body>
[/CODE]

There will be more calls but I thought I would try and get one working first.

My Name.php is:

[CODE]
<?php

$con = mysql_connect(“localhost”,”user”,”password”);
if (!$con){
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“mydb”, $con);

$result=mysql_query(“SELECT * FROM members”);

while($row = mysql_fetch_array($result)){
echo “$a[]='” . $row[‘name’] . “‘;”;
}

$q=$_GET[“q”];

if (strlen($q) > 0){
$hint=””;
for ($i=0; $i<count($a); $i++){
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))){
if ($hint==””){
$hint=$a[$i];
}else{
$hint=$hint . ” , “.$a[$i];
}
}
}
}

if ($hint == “”){
$response=”no suggestion”;
}else{
$response=$hint;
}

echo $response;
?>
[/CODE]

Any guidance would be fantastic.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 14.2010 — <i>
</i>function showHint(str,[COLOR="Red"]FILE+str[/COLOR],ID)
...
onkeyup="showHint('getNames.php?q=','txtHintName')"
...

You sent 2 arguments in handler, but you expect 3 in the function? Moreover, the arguments in a function are the direct replacements of the arguments sent. The construction [B]File+str[/B] will be undefined and anyway it is illegal. It is not like in PHP. In JavaScript the received arguments are in strict biunivocal relationship with the sent arguments. JavaScript does not allow operations, nor any other statement within the functions' receiving zone of the arguments.
Copy linkTweet thisAlerts:
@JorcaauthorOct 14.2010 — Thank you I see your point.

The code I started with just had (str), but I wanted to create one function that could be called by various events.

(File,ID) would fit with the handler, but how would I indicate that the string needs to be passed?
×

Success!

Help @Jorca spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...