Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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的链接? func1(){}; func2(){};_Javascript_Php_Html - Fatal编程技术网

如何知道在php中单击了指向JavaScript的链接? func1(){}; func2(){};

如何知道在php中单击了指向JavaScript的链接? func1(){}; func2(){};,javascript,php,html,Javascript,Php,Html,如何知道在php中单击了哪个链接?php是一种服务器端语言。如果您想让服务器知道客户端点击了链接,可以发出ajax请求 <script type="text/javascript"> func1(){}; func2(){}; </script> <body> <a href="javascript:func1();">some text</a> <a href="javascript:func2();">some tex

如何知道在php中单击了哪个链接?

php是一种服务器端语言。如果您想让服务器知道客户端点击了链接,可以发出ajax请求

<script type="text/javascript">
func1(){};
func2(){};
</script>

<body>
<a href="javascript:func1();">some text</a>
<a href="javascript:func2();">some text</a>
</body>

函数callAjax(url,回调){
var-xmlhttp;
//与IE7+、Firefox、Chrome、Opera和Safari兼容
xmlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
回调(xmlhttp.responseText);
}
}
open(“GET”,url,true);
xmlhttp.send();
}
函数func(id){
callAjax('some_file.php?id=func'+id,函数(res){alert(res);})
};
some_file.php:

<html>
    <body>
        <script type="text/javascript">
            function callAjax(url, callback){
                var xmlhttp;
                // compatible with IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function(){
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                        callback(xmlhttp.responseText);
                    }
                }
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }

            function func(id){
                callAjax('some_file.php?id=func'+id, function(res){alert(res);})
            };
        </script>

        <a href="javascript:func(1);">some text</a>
        <a href="javascript:func(2);">some text</a>
    </body>
</html>


到目前为止您尝试了什么?1。为每个链接使用一个标识符。2.在单击处理程序中使用HTTP获取标识符并将其传递给PHP。完成了@Vohuman不要喂它:P@fubbe那只是一点点而已:)危险:这个密码是。用户输入在插入HTML文档之前需要转义!。是的,但这只是一个简单的例子(他可能正在用localhost测试这个)。但是很好,我现在添加了htmlspecialchars来防止xss。
<?php
    $id = @$_GET['id'];
    if (!isset($id))
        $id = "";
    echo "You clicked on link " . htmlspecialchars($id, ENT_QUOTES, 'UTF-8') . ".";
?>