Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should change your query like this:</p> <pre><code>$query = mysql_query("SELECT pID, lname, fname FROM Professor WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'"); </code></pre> <p>Please make sure this these fields in json and javascript are the same:</p> <pre><code>$arr[] = array( "id" =&gt; $row["pID"], "last" =&gt; $row["lname"], "first" =&gt; $row["fname"] ); $("div#content").append("- &lt;a href='post.php?id=" + this.id + "'&gt;" + this.first + " " + this.last + "&lt;/a&gt;"); </code></pre> <p>HTML code:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;jQuery Search Demonstration&lt;/title&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $(".keywords").keyup(function(){ getData(); }); $(".table").click(function(){ getData(); }); }); function getData(){ $.post("search.php", { keywords: $(".keywords").val(), table: $('.table:checked').val() }, function(data){ $("div#content").empty(); var phppage; switch($('.table:checked').val()) { case 'professor': phppage = 'prof'; break; case 'department': phppage = 'department'; break; case 'course': phppage = 'course'; break; } $.each(data, function(){ $("div#content").append("- &lt;a href='" + phppage + ".php?pID=" + this.id + "'&gt;" + this.name + "&lt;/a&gt;"); }); }, "json"); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; Search by: &lt;input type="text" name="search" class="keywords" /&gt;&lt;br /&gt; &lt;input type="radio" name="table" class="table" value="professor" checked="checked" /&gt; Professor&lt;br /&gt; &lt;input type="radio" name="table" class="table" value="department" /&gt; Department&lt;br /&gt; &lt;input type="radio" name="table" class="table" value="course" /&gt; Course&lt;br /&gt; &lt;div id="content"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>PHP code:</p> <pre><code>&lt;?php $link = mysql_connect('localhost',"#","#"); mysql_select_db("#", $link); $arr = array(); $keywords = mysql_real_escape_string( $_POST["keywords"] ); switch ($_POST["table"]) { case 'professor'; $arr = getProfessor($keywords); break; case 'department'; $arr = getDepartment($keywords); break; case 'course'; $arr = getCourse($keywords); break; } echo json_encode( $arr ); function getProfessor($keywords){ $arr = array(); $query = mysql_query("SELECT pID, lname, fname FROM Professor WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'"); while( $row = mysql_fetch_array ( $query ) ) { $arr[] = array( "id" =&gt; $row["pID"], "name" =&gt; $row["fname"] . ' ' . $row["lname"]); } return $arr; } function getDepartment($keywords){ $arr = array(); $query = mysql_query("SELECT prefix, code FROM Department WHERE name LIKE '%". $keywords . "%'"); while( $row = mysql_fetch_array ( $query ) ) { $arr[] = array( "id" =&gt; $row["code"], "name" =&gt; $row["code"]); } return $arr; } function getCourse($keywords){ $arr = array(); $query = mysql_query("SELECT prefix, code FROM Course WHERE CONCAT(prefix,course) LIKE '%". $keywords . "%'"); while( $row = mysql_fetch_array ( $query ) ) { $arr[] = array( "id" =&gt; $row["code"], "name" =&gt; $row["code"]); } return $arr; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload