所以我需要做的是使用ajax从文本框中取名,将其提交到php脚本然后打印php的结果。
这是我到目前为止所拥有的:
<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>
这是我的ajax:
<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>
现在,PHP脚本中发生的一切就是它使用名为'checkname'的文本框中的名称(使用POST)查询sql db,然后它回显与该名称相关的段落。我知道php脚本在没有AJAX的情况下通过全面测试工作。
因此,根据我现在的情况,它会从URL中的文本框中发送名称,但会将coddisp
div留空。
任何想法的人?
<?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\n", 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);
}
?>
编辑:添加了PHP代码
答案 0 :(得分:0)
试试这个,你遗漏了checkname()
函数中的一些内容,你的表单需要停止默认操作:
<?php
//example check.php
if( $_SERVER['REQUEST_METHOD'] == 'POST' &&
isset($_POST['checkname']))
{
echo htmlspecialchars($_POST['checkname']).' was sent as POST and received within PHP and now its been returned.';
die;
} ?>
<form action="" method="POST" onsubmit="return namecheck(this);">
Are you in the deathnote? <br/>
Name: <textarea type="text" name="checkname"></textarea>
<input type="submit" value="Submit">
</form>
<div id="coddisp"> Nothing yet </div>
<script>
function namecheck(form)
{
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("POST","./check.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("checkname="+escape(form.checkname.value));
return false;
}
</script>
以下是与jQuery相同的东西
<?php
//example check.php, in this we check for bob and return as json
if( $_SERVER['REQUEST_METHOD'] == 'POST' &&
isset($_POST['checkname']))
{
if($_POST['checkname'] == 'bob'){
$resp = array('status'=>true,
'result'=>htmlspecialchars($_POST['checkname']).' found yada.');
}else{
$resp = array('status'=>false,
'result'=>htmlspecialchars($_POST['checkname']).' not found.');
}
//set json header
header('Content-Type: application/json');
//send respoce
echo json_encode($resp);
die;
} ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<form action="" method="POST" id="checkname_form">
Are you in the deathnote? <br/>
Name: <textarea type="text" name="checkname"></textarea>
<input type="submit" value="Submit">
</form>
<div id="coddisp"> Type bob above... </div>
<script>
$(function(){
/* Post form */
$("#checkname_form").submit(function(event){
event.preventDefault();
var request = $.ajax({
type: "POST",
url: "./check.php",
data: $(this).serialize(),
dataType: "json"
});
request.done(function(data) {
//check on data.status == true
if(data.status == true){
$("#coddisp").html(data.result);
}else{
//you could do something else here
$("#coddisp").html(data.result);
}
});
request.fail(function(jqXHR, textStatus, errorThrown) {
$("#coddisp").html('Error posting form');
});
});
});
</script>
</body>
</html>
答案 1 :(得分:0)
实际上你的代码是正确的。问题是您没有指定要发送的数据:
xmlhttp.open("GET", "check.php", true);
XMLHttpRequest对象将执行对 check.php 的HTTP请求,这就是全部。 如果你想通过GET发送数据,只需像在URL中那样传递它们:
xmlhttp.open("GET", "check.php?foo=bar&number=1337", true);
如果您执行上述请求,PHP脚本将在$ _GET中包含这些数据:
array(
'foo' => 'bar',
'number' => '1337', // Note it's still a string, you must cast it as int
)
因此,如果您想从表单中发送名称,请写下:
xmlhttp.open("GET", "check.php?name=" + escape(name), true);
其中 name 是包含值的变量。