Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过AJAX发送链接_Javascript_Php_Ajax - Fatal编程技术网

Javascript 通过AJAX发送链接

Javascript 通过AJAX发送链接,javascript,php,ajax,Javascript,Php,Ajax,我试图通过ajax检查数据库中的用户。如果成功,它将返回一个链接,该链接的用户名为链接名。之前没有链接部分,一切正常,但现在显示“未定义”。我还知道我使用的是不推荐的mysql\u*函数,我的代码很容易被sql注入,但我担心的是上面提到的问题 这是通过AJAX返回的php代码 <?php header('Content-Type: text/xml'); echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

我试图通过ajax检查数据库中的用户。如果成功,它将返回一个链接,该链接的用户名为链接名。之前没有链接部分,一切正常,但现在显示“未定义”。我还知道我使用的是不推荐的
mysql\u*
函数,我的代码很容易被sql注入,但我担心的是上面提到的问题

这是通过AJAX返回的php代码

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
require_once("includes/connection.php");
echo '<response>';
$user=$_GET['user'];

if($user==""){
echo "type the username";
}
else{   
$query="SELECT email_id FROM users
    WHERE email_id='{$user}'
    ";
$user_result = mysql_query($query,$connection);
if(mysql_num_rows($user_result)>0){
echo "<a href='friend.php?friend_id=1'> {$user} </a> exists";
    }
else{
                echo "no such user as {$user} exists";
    }
}

echo '</response>';
?>

客户端JavaScript需要正确提取a标记,并将其插入主页面

请参见下面的示例:

<!doctype html>
<html>
<body>
<input type="text" id="user_input">
<button id="submit" onclick="start()">Search for user</button>
<div id="divD">
</div>
<script>
function start()
{
xmlHttp = new XMLHttpRequest();
if(xmlHttp){
try{
    if(xmlHttp.readyState==0 || xmlHttp.readyState==4)
    {
        user= encodeURIComponent(document.getElementById("user_input").value);
        xmlHttp.open("GET","search.php?user="+user,true);
        xmlHttp.onreadystatechange = mainFunctionHandler;
        xmlHttp.send(null);
    }else{
        setTimeout('start()',1000);
    }
}catch(e){
    alert(e.toString());
        }
}
}

function mainFunctionHandler()
{
if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
        xmlResponse = xmlHttp.responseXML;
        xmlDocumentElement = xmlResponse.documentElement;
        message = xmlDocumentElement.innerHTML;
        document.getElementById("divD").innerHTML=message;
        setTimeout('start()',1000);
    }else{
        alert("something went wrong");
    }
}
}
</script>
</body>
</html>

搜索用户
函数start()
{
xmlHttp=新的XMLHttpRequest();
if(xmlHttp){
试一试{
if(xmlHttp.readyState==0 | | xmlHttp.readyState==4)
{
user=encodeURIComponent(document.getElementById(“用户输入”).value);
open(“GET”、“search.php?user=“+user,true”);
xmlHttp.onreadystatechange=mainFunctionHandler;
xmlHttp.send(空);
}否则{
setTimeout('start()',1000);
}
}捕获(e){
警惕(如toString());
}
}
}
函数mainFunctionHandler()
{
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse=xmlHttp.responseXML;
xmlDocumentElement=xmlResponse.documentElement;
message=xmlDocumentElement.innerHTML;
document.getElementById(“divD”).innerHTML=消息;
setTimeout('start()',1000);
}否则{
警惕(“出了差错”);
}
}
}

1。在顶部2写入错误报告(E_ALL)。检查是否建立了连接。3.从mysql_查询中删除$connection变量,如果仍然不起作用,请将您的所有connection.php数据放在上述代码的顶部,然后再次检查。在返回纯文本之前,现在返回的是一个
标记,该标记被解释为xml节点。提取xml数据的代码必须更改。您在ajax成功回调函数中做什么?请提供代码。连接已建立,ajax正在显示其他两个MSG@Nomani已经包含了上面的ajax代码。建议更正@James实际上我对ajax是新手。所以我不能完全理解你的代码。你介意检查一下我上面包含的ajax代码吗@jeyoor@decoder15我已经编辑了一个工作示例。对代码所需的唯一更改是mainFunctionHandler()中,正如您在代码中提到的,我将.firstchild.data更改为.innerHTML。但它不起作用。当用户存在时,它仍然表示“未定义”。否则就行了@Jeyoor这里有一个我的代码示例如果上面没有帮助,你能给我指一个你的代码示例吗@解码器15
<!doctype html>
<html>
<body>
<input type="text" id="user_input">
<button id="submit" onclick="start()">Search for user</button>
<div id="divD">
</div>
<script>
function start()
{
xmlHttp = new XMLHttpRequest();
if(xmlHttp){
try{
    if(xmlHttp.readyState==0 || xmlHttp.readyState==4)
    {
        user= encodeURIComponent(document.getElementById("user_input").value);
        xmlHttp.open("GET","search.php?user="+user,true);
        xmlHttp.onreadystatechange = mainFunctionHandler;
        xmlHttp.send(null);
    }else{
        setTimeout('start()',1000);
    }
}catch(e){
    alert(e.toString());
        }
}
}

function mainFunctionHandler()
{
if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
        xmlResponse = xmlHttp.responseXML;
        xmlDocumentElement = xmlResponse.documentElement;
        message = xmlDocumentElement.innerHTML;
        document.getElementById("divD").innerHTML=message;
        setTimeout('start()',1000);
    }else{
        alert("something went wrong");
    }
}
}
</script>
</body>
</html>