在AJAX POST请求到同一页面时失败(带有示例) [英] Failed at AJAX POST request to the same page (with example)

查看:62
本文介绍了在AJAX POST请求到同一页面时失败(带有示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

####### The name of this file is 'test_ajaxfn.php' #######
<?php
if($_POST['var']) {
    $result = $_POST['var'];
    echo $result.' is successfully passed to the same page using Ajax Post. :)';    
} else {
    echo 'There is no POST variable passed to the same page. :( ';  
}

echo '<br> Above indicates the ajax post function \'.\' ';
?>


<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>


<script>
$(document).ready(function() {
    $('#clickdiv').click(function() {
        var sth = "value to send to PHP";
        $.post(
        'test_ajaxfn.php',
        {"var":"1234567"},
        function(){
            alert('success');
            $('#clickdiv').text('the POST variable should be posted.');
        }
        );

    });
});
</script>

</head>

<body>
<div id="clickdiv" style="width: 50px; height: 50px; background:#CC3300; cursor:pointer;">
</div>
</body>
</html>

说明

我想将POST变量传递到同一页面并使用AJAX加载它.但是,我没有这样做.我无法使用POST方法将"var"传递给$ _POST ['var'].因此,我无法显示以下内容: 回声$结果.已使用Ajax Post成功传递到同一页面. :)';

Description

I want to pass POST variable to the same page and load it using AJAX. However, I failed to do so. I can't pass the "var" to $_POST['var'] using POST method. Hence I can't display the following: echo $result.' is successfully passed to the same page using Ajax Post. :)';

我做错了什么?

我实际上是在尝试解决这个问题: 计算Google输入地址的距离以及使用jQuery ajax.get 从MySQL Server获得的所有地址 现在,我简化了我的问题,但仍然行不通.请帮助:(

I was actually trying to tackle this problem: Calculate Google distance of Input address and all the address from MySQL Server using jQuery ajax.get Now I simplify my problem but still it doesn't work. Please help :(

推荐答案

如果将其发布到同一页面,服务器将创建一个新进程来处理请求.您没有看到任何输出的原因是因为服务器正在看到文件的发布,旋转实例并执行php脚本,但这与您的php线程不同.将您的脚本更改为此,以查看您的成功文本.

If you post it to the same page the server creates a new process to handle the request. The reason you are not seeing any output is because the server is seeing a post to a file, spinning up an instance and executing the php script but that is different from your php thread. Change your script to this to see your success text.

<script>
$(document).ready(function() {
    $('#clickdiv').click(function() {
        var sth = "value to send to PHP";
        $.post(
        'test_ajaxfn.php',
        {"var":"1234567"},
        function(result){
            alert(result);
            $('#clickdiv').text('the POST variable should be posted.');
        }
        );

    });
});
</script>

这篇关于在AJAX POST请求到同一页面时失败(带有示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆