Using AJAX, Javascript and PHP with MySQL to display search results -
my aim use ajax display php search results without having reload page. far have been able results, (i'm new ajax, , not know jquery), problem search results displaying in html table appear @ top page above everything, not in specified div. have used innerhtml try , display correctly.
here main code:
function searchresults(title) { if (title == "") { document.getelementbyid("response").innerhtml=""; } var request= new xmlhttprequest(); request.onreadystatechange=function() { if (request.readystate == 4 && request.status == 200) { var displaydiv= document.getelementbyid("response"); displaydiv.innerhtml=request.responsetext; } } request.open("get", "functions.php?titlea="+title, true); request.send(); document.getelementsbyid("response").innerhtml="fuck"; } </script> <title>anime search</title> </head> <body> <div class="main"> <div class= "header"> <h1>search database</h1> </div> <!-- close header --> <div class= "searcha"> <p>search anime database below</p> <form onsubmit="searchresults(titlea)"> <label>title</label><input type="text" name="titlea" placeholder="enter title"/> <input type="submit" value="submit"/> </form> <div id="response"> </div> <!-- close response --> </div> <!-- close searcha --> and here php:
if (isset($_get["titlea"])) { $title= $_get["titlea"]; $connection= connect(); $username= $_session["username"]; $tablea= $username . "_anime"; $querya= "select * anime." . "`$tablea` title '%$title%'"; $resulta= mysqli_query($connection, $querya); if ($resulta == false) { die("no results found"); } $numrows= mysqli_num_rows($resulta); echo "<table class= 'tsearch'> <thead> <th>title</th> <th>alt title</th> <th>seasons</th> <th>episodes</th> <th>ova's</th> <th>movies</th> <th>status</th> <th>downloaded</th> </thead> <tbody>"; while($row= mysqli_fetch_array($resulta, mysqli_both)) { echo "<tr>"; echo "<td>" . $row["title"] . "</td>"; echo "<td>" . $row["alt_title"] . "</td>"; echo "<td>" . $row["seasons"] . "</td>"; echo "<td>" . $row["total_episodes"] . "</td>"; echo "<td>" . $row["ovas"] . "</td>"; echo "<td>" . $row["movies"] . "</td>"; echo "<td>" . $row["status"] . "</td>"; echo "<td>" . $row["downloaded"] . "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table>"; mysqli_close($connection); if ($resulta == false) { echo mysqli_error($connection); } } i have of course spend many hours trying find out whats wrong, plan on learning jquery, want work, please not tell me use jquery,
edit: link screenshot: http://i.imgur.com/ejkhati.png browser safari 7.0.4, tried firefox , got same problem thank in advance, rainy
here, let me give generic layout:
this may easiest way use ajax, use in every project. first link external ajax.js can use below script.
i don't know have done wrong base on description, works me locally. 1 reason may #response isn't loaded yet when execute script.
ajax.js
function ajaxobj( meth, url ) { var x; if (window.xmlhttprequest) { //new browsers x = new xmlhttprequest(); } else { //ie5, ie6 x = new activexobject("microsoft.xmlhttp"); } x.open( meth, url, true ); x.setrequestheader("content-type", "application/x-www-form-urlencoded"); return x; } function ajaxreturn(x){ if(x.readystate == 4 && x.status == 200){ return true; } } script tag request ajax:
var ajax = ajaxobj("get", "functions.php"); ajax.onreadystatechange = function() { if(ajaxreturn(ajax) == true) { document.getelementbyid("response").innerhtml=ajax.responsetext; } } ajax.send("titlea="+title); or if prefer jquery:
//you need load jquery first before using $(function() { //this line means when document ready var ajax = ajaxobj("get", "functions.php"); ajax.onreadystatechange = function() { if(ajaxreturn(ajax) == true) { $("#response").html(ajax.responsetext); } } ajax.send("titlea="+title); });
Comments
Post a Comment