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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化