Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL/PHP Search Engine - Without Refresh? Examples and documents provided
    primarykey
    data
    text
    <p>My question is rather lengthy, so I'll get right to it. I have a search engine set up that sends data (GET method) to a php script which queries a MySQL database and then displays the results. You can view the project, to get a better understanding, here: <a href="http://www.quinterest.org/testcategory/" rel="nofollow">http://www.quinterest.org/testcategory/</a></p> <p>The form consist of a search box as well as a multiple select option that narrow the search.</p> <p>Everything thing works wonderfully, but there is one thing I would like to change and I don't know how. If someone could explain, in detail, what I need to do to be able to show the results (I figure it will be in a div written in the HTML?) from the query on the same page as the original form without the need to refresh the page?</p> <p>If you would like an example, here is one: <a href="http://quizbowldb.com/search" rel="nofollow">http://quizbowldb.com/search</a> The search stays on the same page.</p> <p>P.S. I know the mysql_ functions are outdated. Please don't harass me. I'm all very new to this still and the mysql_ functions are simple, easy and good enough for what I need. When I get further programming experience (I'm still in middle school), I may convert it to PDO or MySQLi.</p> <p><strong>HTML Form:</strong></p> <pre><code> &lt;form action='search.php' method='GET' class="form-search"&gt; &lt;input type='text' placeholder="What would you like to learn about?" class="input-xxlarge search-query" id="inputInfo" name='search'&gt;&lt;br&gt;&lt;/br&gt; &lt;input type='submit' class="btn btn-large btn-primary" name='submit' value='Search'&gt;&lt;/br&gt; &lt;select multiple="multiple" name='category[]'&gt; &lt;option value="%"&gt;All&lt;/option&gt; &lt;option&gt;Literature&lt;/option&gt; &lt;option&gt;History&lt;/option&gt; &lt;option&gt;Science&lt;/option&gt; &lt;option&gt;Fine Arts&lt;/option&gt; &lt;option&gt;Trash&lt;/option&gt; &lt;option&gt;Mythology&lt;/option&gt; &lt;option&gt;Phylosophy&lt;/option&gt; &lt;option&gt;Social Science&lt;/option&gt; &lt;option&gt;Religion&lt;/option&gt; &lt;option&gt;Geography&lt;/option&gt; &lt;/select&gt; &lt;/center&gt; &lt;/form&gt; </code></pre> <p><strong>search.php</strong></p> <pre><code>&lt;?php $button = $_GET ['submit']; $search = $_GET ['search']; print $search; //validating search term length and connecting to db if(strlen($search)&lt;=1) echo "Search term too short"; else{ echo "You searched for &lt;b&gt;&lt;em&gt;$search&lt;/em&gt;&lt;/b&gt; and "; mysql_connect("fake","fake","fake"); mysql_select_db("quinterestdb");} //validating search term for protection; if statement to avoid errors being displayed if (strlen($search)&gt;1){ mysql_real_escape_string($search);} //exploding search with multiple words $search_exploded = explode (" ", $search); //creates array of all terms in search foreach($search_exploded as $search_each) //loops through array { $x++; if($x==1) $construct .="Answer LIKE '%$search_each%'"; //if only one value in array else $construct .="AND Answer LIKE '%$search_each%'"; //for each multiple value in array } $cat = $_GET ['category']; //preparing array (multiple choices) if (is_array($cat)) { foreach($cat as $val) // { if($val=="%") //if no category is selected continue; else //split array choices (separated by ' and ,) $comma_separated = implode("','", $cat); $newvar = "AND Category IN('$comma_separated')"; //if category is chosen } } //ignore for now $constructs ="SELECT * FROM tossupsdb WHERE $construct $newvar"; //completed search query //quering the database; if statement to avoid errors being displayed if (strlen($search)&gt;1){ $run = mysql_query($constructs);} print "$constructs"; //ignore for now //number of results found; if statement to avoid errors being displayed if (strlen($search)&gt;1){ $foundnum = mysql_num_rows($run);} if ($foundnum==0) echo "Sorry, there are no matching result for &lt;b&gt;$search&lt;/b&gt;.&lt;/br&gt;&lt;/br&gt;1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website'&lt;/br&gt;2. Try different words with similar meaning&lt;/br&gt;3. Please check your spelling"; else { echo " &lt;span class='badge badge-info'&gt; $foundnum &lt;/span&gt; results were found:&lt;hr size='5'&gt;"; $per_page = 25; //preparing for pagination; results that appear per page $start = $_POST['start']; //where to start results on page $max_pages = ceil($foundnum / $per_page); //number of pages there will be if(!$start) //starting at 0 $start=0; $getquery = mysql_query("SELECT * FROM tossupsdb WHERE $construct $newvar LIMIT $start, $per_page"); while($runrows = mysql_fetch_array($getquery)) //fetching results { $answer = $runrows ['Answer']; //obtaining individual data from database $category = $runrows ['Category']; //obtaining individual data from database $num = $runrows ['Question #']; //obtaining individual data from database $difficulty = $runrows ['Difficulty']; //obtaining individual data from database $question = $runrows ['Question']; //obtaining individual data from database $round = $runrows ['Round']; //obtaining individual data from database $tournament = $runrows ['Tournament']; //obtaining individual data from database $year = $runrows ['Year']; //obtaining individual data from database //what will be displayed on the results page echo "&lt;div class='alert alert-info' style='border-radius: 20px'&gt;&lt;div style='padding: 10px'&gt; &lt;span class='label label-info' font-size='30px'&gt;&lt;em&gt;Tournament | Year | Round | Question # | Category&lt;/em&gt;&lt;/span&gt;&lt;/div&gt; &lt;b&gt;$tournament |&lt;/b&gt; &lt;b&gt;$year |&lt;/b&gt; &lt;b&gt;$round |&lt;/b&gt; &lt;b&gt;$num |&lt;/b&gt; &lt;b&gt;$category&lt;/b&gt; &lt;p&gt;&lt;em&gt;Question:&lt;/em&gt; $question&lt;/p&gt; &lt;div class='alert alert-info'&gt;&lt;em&gt;&lt;strong&gt;ANSWER:&lt;/strong&gt;&lt;/em&gt; $answer&lt;/div&gt;&lt;/div&gt;&lt;hr&gt; "; } //Pagination Starts echo "&lt;center&gt;"; $prev = $start - $per_page; $next = $start + $per_page; $adjacents = 3; $last = $max_pages - 1; if($max_pages &gt; 1) { //previous button if (!($start&lt;=0)) echo " &lt;a class='btn btn-primary btn-large' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$prev'&gt;Prev&lt;/a&gt; "; //pages if ($max_pages &lt; 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { $i = 0; for ($counter = 1; $counter &lt;= $max_pages; $counter++) { if ($i == $start){ echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;&lt;b&gt;$counter&lt;/b&gt;&lt;/a&gt; "; } else { echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;$counter&lt;/a&gt; "; } $i = $i + $per_page; } } elseif($max_pages &gt; 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if(($start/$per_page) &lt; 1 + ($adjacents * 2)) { $i = 0; for ($counter = 1; $counter &lt; 4 + ($adjacents * 2); $counter++) { if ($i == $start){ echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;&lt;b&gt;$counter&lt;/b&gt;&lt;/a&gt; "; } else { echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;$counter&lt;/a&gt; "; } $i = $i + $per_page; } } //in middle; hide some front and some back elseif($max_pages - ($adjacents * 2) &gt; ($start / $per_page) &amp;&amp; ($start / $per_page) &gt; ($adjacents * 2)) { echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=0'&gt;1&lt;/a&gt; "; echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$per_page'&gt;2&lt;/a&gt; .... "; $i = $start; for ($counter = ($start/$per_page)+1; $counter &lt; ($start / $per_page) + $adjacents + 2; $counter++) { if ($i == $start){ echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;&lt;b&gt;$counter&lt;/b&gt;&lt;/a&gt; "; } else { echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;$counter&lt;/a&gt; "; } $i = $i + $per_page; } } //close to end; only hide early pages else { echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=0'&gt;1&lt;/a&gt; "; echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$per_page'&gt;2&lt;/a&gt; .... "; $i = $start; for ($counter = ($start / $per_page) + 1; $counter &lt;= $max_pages; $counter++) { if ($i == $start){ echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;&lt;b&gt;$counter&lt;/b&gt;&lt;/a&gt; "; } else { echo " &lt;a class='btn' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$i'&gt;$counter&lt;/a&gt; "; } $i = $i + $per_page; } } } //next button if (!($start &gt;=$foundnum-$per_page)) echo " &lt;a class='btn btn-primary btn-large' href='search.php?search=$search&amp;submit=Search+source+code&amp;start=$next'&gt;Next&lt;/a&gt; "; } echo "&lt;/center&gt;"; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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