Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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 使用Jquery Post方法将数据传递到同一页面_Php_Jquery_Post - Fatal编程技术网

Php 使用Jquery Post方法将数据传递到同一页面

Php 使用Jquery Post方法将数据传递到同一页面,php,jquery,post,Php,Jquery,Post,我是Jquery的新手,我的问题很简单,我正在尝试使用Jquery Post方法传递数据,我读了很多,但我无法理解: <!DOCTYPE html> <html> <head> </head> <body> <div class="TestAd" id="TestAd"> <iframe data-aa='58593' src='https://ad.a-ads.com/58593?size=468x60' scro

我是Jquery的新手,我的问题很简单,我正在尝试使用Jquery Post方法传递数据,我读了很多,但我无法理解:

<!DOCTYPE html>
<html>
<head>

</head>
<body>
<div class="TestAd" id="TestAd">
<iframe data-aa='58593' src='https://ad.a-ads.com/58593?size=468x60' scrolling='no' style='width:468px; height:60px; border:0px; padding:0;overflow:hidden' allowtransparency='true' frameborder='0'></iframe>
</div>

<button>Send request</button>
<br>
<?php
if(!empty($_POST["block"])) {
         echo "Ads Are Blocked";

    }
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var height = $('.TestAd').height();
$("button").click(function()
{      
      if (height==0)
      {
       $.post("", {block:true}); 
      }
}     
</script>
</body>
</html>

发送请求

变量高度=$('.TestAd').height(); $(“按钮”)。单击(函数() { 如果(高度==0) { $.post(“,{block:true}); } }

该脚本是一个简单的AdBlocker检查器,感谢您的帮助

将您的PHP更改为:

<?php
if(isset($_POST["block"])) {
         echo "Ads Are Blocked";

}
?>

并将jQuery更改为:

<script>
var height = $('.TestAd').height();
$("button").click(function () {
    if (height == 0) {
        $.post("somepage.php",{block: true}, function (data) {
            // data is the response    
            // do something with it here 
            alert(data);
        });
    }
}
</script>

变量高度=$('.TestAd').height();
$(“按钮”)。单击(函数(){
如果(高度==0){
$.post(“somepage.php”,{block:true},函数(数据){
//数据就是回应
//在这里做点什么
警报(数据);
});
}
}
下面是一些文档,本质上就是你的方式,忽略响应。你必须将匿名函数(
function(data){}
)回调作为第三个参数传递才能处理响应

从文档中: 示例: 请求test.php页面并发送一些附加数据(同时仍然忽略返回结果)

$.post(“test.php”,{name:“John”,time:“2pm”})



如果你想将它重定向到同一页,为什么不使用简单表单标记传递块值。默认情况下,它会在同一页上重定向它

因此,嗯,什么不起作用。你似乎做得不错,但你没有听到帖子的回复。你想看到什么才能说它起作用?我没有收到来自因此,即使高度==0,我也不会得到“广告被阻止”,所以我不知道我是传递了错误的数据还是读取了错误的数据
<form method="post">
<input type="hidden" value="true" name="block">
<input type="submit" value="Send request">
</form>
<?php
if(isset($_POST["block"])) {
         echo "Ads Are Blocked";

}
?>