javascript - Using HTML forms values for AJAX/PHP MySqli query -
i trying use ajax both php , mysqli results database. new ajax , javascript appriciated
this form:
<form method="get"> <input type="number" min=0 name="gameid" id="gameid" placeholder="game id" required><br> <input type="number" min=0 step=0.01 name="price" id="price" placeholder="price" required><br> <button type="submit" onclick="getgames()">submit</button> </form>
this ajax in js:
function getgames() { var gameid = document.getelementbyid("gameid").value; var price = document.getelementbyid("price").value; if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { document.getelementbyid("jumbotron").innerhtml = xmlhttp.responsetext; } }; xmlhttp.open("get","getgames.php?gameid="+gameid+"&price="+price, true); xmlhttp.send(); }
and php
<?php $servername = "localhost"; $serverusername = "root"; $dbname = "ryangames"; $gameid = intval($_get["gameid"]); $price = intval($_get["price"]); $conn = mysqli_connect($servername, $serverusername, "", $dbname); if (!$conn) { die("connection failed: " . mysqli_connect_error()); } $sql = "select * games gameid = ".$gameid." , price = ".$price; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { echo "<div class=\"gameholder\"> <img class=\"gameimg\" src=\"images/".$row["code"].".png\" alt=\"".$row["name"]."\"> <div class=\"gametext\"> <h3 class=\"gamename\">".$row["name"]."</h3> <p class=\"gamedesc\">".$row["description"]."</p>"; if ($row["price"] == 0) { echo "<p class=\"gameprice\">free</p>"; } else { echo "<p class=\"gameprice\">£".$row["price"]."</p>"; } echo" </div> </div>"; } } else { echo "0 results"; } mysqli_close($conn); ?>
i can see values in address nothing happens. query works if include in php , make user refresh every time.
update: changed both getelementbyids have .value on end
change button input type="submit"
input type="button"
Comments
Post a Comment