我想编写一个代码,所以当我在我的表单中插入一个字符时,那么用户(在我的数据库中)的名字以我插入的字符开头显示(就像facebook一样) 但我的问题是当我插入一个字符,而不是只打印用户名,我的页面中的xeroxes就像这样: (我知道我曾经问过这个问题,但我没有得到任何答案,我真的要解决这个问题)
这里是我的代码:
<div id="mymessages">
<div>
<br><br>
<div style="display:inline; border:1px solid gray; margin-left:8px;" onClick="myinbox()"><b>پیام های من</b></div>
<div style="display:inline; border:1px solid gray; margin-left:8px;"><b onClick="sendnewmsg()">ارسال پیام جدید</b></div>
</div>
<script>
function myinbox(){
document.getElementById("myinbox").style.display="block";
document.getElementById("sendnewmsg").style.display="none";
}
function sendnewmsg(){
document.getElementById("sendnewmsg").style.display="block";
document.getElementById("myinbox").style.display="none";
}
</script>
<div id="sendnewmsg">
<form enctype="multipart/form-data" id="sendmessage" style="text-align:center;" name="sendmessage" method="post" action="userpanel.php">
TO: <input onKeyUp="selrec(this.value)" type="text" id="receiver" name="to"><br><br>
<script>
function selrec(name)
{
if(name.length==0)
{
document.getElementById('selectrec').innerHTML="no suggestion";
return;
}
if(window.XMLHttpRequest)
myrequest=new XMLHttpRequest();
else
myrequest=new ActiveXObject("Microsoft.XMLHTTP");
myrequest.onreadystatechange=function()
{
if (myrequest.readyState==4 && myrequest.status==200)
{
document.getElementById('selectrec').innerHTML=myrequest.responseText;
}
}
myrequest.open("GET","userpanel.php",true);
myrequest.send("typedname="+name);
}
</script>
<div id="selectrec">
<?php
if(isset($_POST['typedname'])){
$conn=mysqli_connect('localhost','root');
if (!$conn)
die("couldn't connect" . mysql.error());
$typedname=$_REQUEST['typedname'];
mysqli_select_db($conn,'swimsafe');
$bringusers='SELECT * FROM users WHERE firstname LIKE"'.$typedname.'%" OR lastname LIKE"'.$typedname.'%" LIMIT 3';
$getuserstyped=mysqli_query($conn,$bringusers);
if(!$getuserstyped)
die("couldn't choose the users" .mysql_error());
while($rows=mysqli_fetch_array($getuserstyped))
{
echo $rows['firstname'] . " " . $rows['lastname'];
}
mysqli_close($conn);
}
?>
</div>
SUBJECT: <input type="text" name="subject" ><br><br>
CONTENT:<br> <textarea form="sendmessage" style="height:150px; width:300px;" name="content">
</textarea>
<br><br>
<input type="submit" onClick="return checkreciever()" id="submitmsg" name="submit" value="send">
</form>
</div>
答案 0 :(得分:1)
我相信你有两个问题:
1)混合GET和POST
2)你的文件的php部分需要在html输出之前,如果ajax查询完成,它需要在输出html之前终止。
要解决1),而不是
if(isset($_POST['typedname'])) ...
试
if(isset($_GET['typedname']))...
让我向您展示一个更基本的问题示例 - 一个对服务器进行简单的ajax调用的按钮,并输出服务器要说的内容
<html>
<head>
<script language="javascript">
function ajaxStuff() {
var result = //code to get response from server (you already get this)
//this code posts the typedname like in your code
alert(result);
}
</script>
</head>
<body>
<button name="ajax-button" onclick="ajaxStuff()">Click me to do ajax stuff!</button>
</body>
</html>
<?php
if (isset($_GET['typedname'])) {
echo "Hello, I am the server!";
}
?>
现在当我们点击按钮时,我们希望javascript用字符串提示我们#34;您好,我是服务器!&#34; 但这不会发生。相反,您将获得页面的整个输出,后跟字符串&#34;您好我是服务器!&#34;
为什么呢?每当查询userpanel.php时,所有html代码(包括html标记之间)都将在php代码之前输出。然后,服务器将看到$ _GET [&#39; typedname&#39;]已设置,并吐出&#34; Hello ...&#34;字符串也是。
如果是ajax查询,您希望能够预防服务器的html输出,并且只允许它输出字符串。这意味着您的PHP代码需要在html之前,并且在ajax查询的情况下,应该在输出html之前退出:
<?php
if (isset($_GET['typedname'])) {
echo "Hello, I am the server!";
exit; //THIS IS THE CRUCIAL EXIT STATEMENT.
//It will stop the script here, with no html output.
}
?>
<html>
<head>
<script language="javascript">
function ajaxStuff() {
var result = //code to get response from server (you already get this)
//this code posts the typedname like in your code
alert(result);
}
</script>
</head>
<body>
<button name="ajax-button" onclick="ajaxStuff()">Click me to do ajax stuff!</button>
</body>
</html>
你有它!