I'm trying to have a form, which when submitted returns the output from a simple SQL query onto the page without needing to be reloaded (AJAX). I can get simple outputs to work but when I use the PHP for the SQL query nothing is returned. Any help would be much appreciated. I also can't find anyway to check what is wrong with my Javascript/Php.
Pretty new to web development so apologies if this is trivial. All previously found solutions haven't worked
My Code;
a1.php
<script src='../js/scriptget.js'></script>
<form>
<fieldset>
<legend>Login</legend>
Username:<br>
<input type="text" name="myusername" placeholder="Username">
<br>
Password:<br>
<input type="text" name="mypassword" placeholder="Password">
<br><br>
<input type="submit" value="Submit" onclick='return getAccount();'>
</fieldset>
</form>
scriptget.js
function getAccount(){
var phpOut = $.ajax({
type: 'GET',
url: 'submitInjection.php',
data: 'myusername=billsmith&mypassword=password'
});
drawOutput('hello');
return false;
}
function drawOutput(responseText){
var container = document.getElementById('output2');
container.innerHTML = responseText;
}
submitinjection.php
<?php
$host="localhost"; //Host Name
$username="root"; // MySql Username
$password="root"; // Mysql Password
$db_name="Honours2"; //Database Name
$tbl_name="Users"; // Table Name
// Connect to server and select database
$conn = mysql_connect("$host", "$username", "$password") or die("Cannot Connect");
mysql_select_db("$db_name") or die("Cannot select DB");
// User and Password sent from form
$myusername = $_GET['myusername'];
$mypassword = $_GET['mypassword'];
/**
Protect MYSQL INJECTION
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
*/
$sql = "SELECT *
FROM $tbl_name
WHERE username = '$myusername'
AND password = '$mypassword'
";
$result=mysql_query($sql);
/* echo $sql; */
if (!$result){
die('Invalid Query: ' . mysql_error() . $sql);
}
if ($result){
echo($sql);
}
/* var_dump($result); */
while ($row = mysql_fetch_assoc($result)){
echo $row['username'];
echo ": ";
echo $row['balance'];
}
mysql_free_result($result);
$conn->close();
?>
Thanks in advance
successfunction / handler to your ajax call to process the results.echothe results you want, then in JS AJAX you use asuccessfunction with a variable for the results.