So all i need to do is use ajax to take a name from a text box, submit it to a php script and then print the result of the php.
Here is what i have so far:
<form action="" method="POST">
Are you in the deathnote? <br/>
Name: <textarea type="text" name="checkname"></textarea>
<input type="submit" value="Submit" onclick="namecheck()">
</form>
<div id="coddisp"> Nothing yet </div>
And here is the ajax i have:
<script>
function namecheck() {
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("coddisp").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "check.php", true);
xmlhttp.send();
}
</script>
Now all that happens in the PHP script is that it queries an sql db with the name from the textbox called 'checkname' (using POST) and then it echoes a paragraph related to that name. I know the php script works without the AJAX through thorough testing.
So with what i have right now, it sends off the name from the textbox in the URL, however it leaves the coddisp
div blank.
Any ideas guys?
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST' &&
isset($_POST['checkname']))
{
$link = mysqli_connect('localhost', 'rhoiydsc_testusr', 'Pass123!', 'rhoiydsc_deathnote');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT cod FROM deathnote WHERE victim=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $_POST['checkname']);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $cod);
/* fetch value */
mysqli_stmt_fetch($stmt);
echo $cod;
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
}
?>
EDIT: PHP CODE ADDED