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 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题