dongye1934 2016-03-17 09:19
浏览 56
已采纳

AJAX请求PHP文件,PHP文件不接收

I'm trying to send a AJAX request to a PHP file. The PHP file does not receive the request.

I'm using the following code:

register.php:

$('#button_step1').click(function(e) {
        $.ajax({
            type: 'POST',
            url: '../core/views/cp/test.php',
            data: { message: 'works' },

            success: function(resp) {
                alert(resp);
            }
        });
        $('.step1 input').prop('disabled', true);
        window.location.replace("http://10.0.0.13/core/views/cp/test.php");
    });

test.php:

<?php

echo $_POST['message'];

?>

The message I get after I get redirected to test.php, "Notice: Undefined index: message in C:\xampp\htdocs\core\views\cp\test.php on line 3".

Issue 2:

When I require_once my register_view.php which includes the AJAX request into my register.php and let it send a AJAX request to the register.php file, it will show me the alert() which has all data from the whole PHP file, where it should only show 'works'.

Code,

register.php:

<?php

require_once '../core/init.php';

$_SESSION['message'] = $_POST['message'];
echo $_SESSION['message'];

require_once VIEW_ROOT . '/cp/register_view.php';

register_view.php:

$('#button_step1').click(function(e) {
        $.ajax({
            type: 'POST',
            url: '../core/views/cp/test.php',
            data: { message: 'works' },

            success: function(resp) {
                alert(resp);
            }
        });
        $('.step1 input').prop('disabled', true);
        //window.location.replace("http://10.0.0.13/core/views/cp/test.php");
    });
  • 写回答

1条回答 默认 最新

  • douqi1928 2016-03-17 09:39
    关注

    require_once VIEW_ROOT . '/cp/register_view.php'; // you are loading view there. you have to do something like this

     $a= $_POST['message'];
    print_r(json_encode($a));exit;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒