Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

help to fix jquery problem index.php <div id=\"suggest\"> <input type=\"text\" s

ID: 3635164 • Letter: H

Question

help to fix jquery problem

index.php

<div id="suggest">
<input type="text" size="25" value="" id="country" class="" />

<div class="suggestionsBox" id="suggestions"> <img src="arrow.png" alt="upArrow" />
<div class="suggestionList" id="suggestionsList"> </div>
</div>
</div>

autosuggest.php

<html>
<head>

</head>
<body>
<style type="text/css">
a:link {text-decoration:none; color:white;} /* unvisited link */
a:visited {text-decoration:none; color:white;} /* visited link */
a:hover {text-decoration:none; color:black;} /* mouse over link */
</style>
<?php
mysql_connect("localhost","root","");
mysql_select_db("phplogin");

$Members = mysql_query(" SELECT id FROM users") or die(mysql_error());
$numRowsMembers = mysql_num_rows($Members);
for($count = 1; $count <= $numRowsMembers; $count++)
{
$name = mysql_fetch_array($Members);
$userid=$name['id'];
}
echo $userid;

$db = new mysqli('localhost', 'root' ,'', 'phplogin');

if(!$db) {

echo 'Could not connect to the database.';
} else {

if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryStr…

if(strlen($queryString) >0) {

mysql_connect("localhost","root","…
mysql_select_db("phplogin");

$Members = mysql_query(" SELECT id FROM users") or die(mysql_error());
$numRowsMembers = mysql_fetch_assoc($Members);
$id = isset($_POST['id']);


$query = $db->query("SELECT id, firstname, lastname, email FROM users WHERE firstname LIKE '$queryString%' OR lastname LIKE '$queryString%' LIMIT 10");
if($query) {
echo '<ul>';
while ($result = $query ->fetch_object()) {
echo '<a href="http://localhost/post/member_profi…>'.$re… '.$result->lastname.'
'.$result->email.'</li></a>';
}
echo '</ul>';

} else {
echo 'OOPS we had a problem :(';
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
</body>
</html>

Explanation / Answer

There are a few problems in your script, I'd suggest having someone look over your code before pushing this to a server. The problem when you add a space is that the $queryString var is set like this: $queryString = $db->real_escape_string($_POST['queryStr… (please post the rest if there is more) This doesn't account for spaces, so you query is now looking for someone with a first name that has a space in it, or a last name with a space in it: $query = $db->query("SELECT id, firstname, lastname, email FROM users WHERE firstname LIKE '$queryString%' OR lastname LIKE '$queryString%' LIMIT 10"); Example: If i put in 'Adam H', your querying for a firstname like 'Adam H' or a lastname like 'Adam H' as opposed to 'Adam' with a last name 'H'. The php function you need is explode. Explode the $queryString at spaces like this $queryString = explode(' ',$queryString); This will break up your $queryString at all spaces and store it as an array. In the example, $queryString[0] now equals 'Adam' and $queryString[1] now equals 'H'.