Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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
php中的javascript和使用ajax_Javascript_Php_Ajax - Fatal编程技术网

php中的javascript和使用ajax

php中的javascript和使用ajax,javascript,php,ajax,Javascript,Php,Ajax,我对php、ajax和javascript感到非常困惑。 我正在使用从w3学校获得的一些ajax代码来处理表单并将其显示在表单输入下面。然而,我似乎无法正确使用php和Javascript。我在php标记中使用JavaScript,然后在相同的php标记中获取变量并对其进行响应。然后我尝试在ajax中获取该变量。我认为我没有正确地使用ajax和php获取变量。有人有什么建议吗。 谢谢 这是我的html文档中的javascript <script> function showHint(

我对php、ajax和javascript感到非常困惑。 我正在使用从w3学校获得的一些ajax代码来处理表单并将其显示在表单输入下面。然而,我似乎无法正确使用php和Javascript。我在php标记中使用JavaScript,然后在相同的php标记中获取变量并对其进行响应。然后我尝试在ajax中获取该变量。我认为我没有正确地使用ajax和php获取变量。有人有什么建议吗。 谢谢

这是我的html文档中的javascript

<script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "hackacronymphp.php?phpAnswer=" + str, true);
        xmlhttp.send();
    }
}
</script>

函数showHint(str){
如果(str.length==0){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“hackacronymphp.php?phpAnswer=“+str,true”);
xmlhttp.send();
}
}
下面是我的脚本的最后一部分,其中包含我的php代码,所有代码都在一个php标记中

    var stringe = vari.join("");
     console.log(stringe);
    var answer = dataarray[stringe];
    console.log(answer);
    </script>
    $phpanswer = $_GET['answer']
    echo $phpanswer ;
?>
var-stringe=vari.join(“”);
控制台日志(stringe);
var answer=dataarray[stringe];
控制台日志(应答);
$phpanswer=$\u获取['answer']
echo$phpanswer;
?>

文件“hackacronymphp.php”应查找数据库或从文件中检索数据。您提供的代码看起来好像已经在JavaScript代码(在数组中)中找到了“答案”,因此不需要php/ajax。也希望将来我会使用数据库。我想在提交按钮下显示数据,而不必转到其他页面。有什么建议吗?你想通过ajax动态加载一些东西吗?您应该在php中检索所有内容,并在加载第一个页面时在html中显示它。然后,如果需要,可以使用Ajax动态地检索额外的数据(但如果不需要,则不必处理不必要的额外页面请求)。别忘了,php代码将在服务器端执行,而javascript代码在客户端(即在浏览器中)执行。