doujing8435 2014-11-10 00:38
浏览 62
已采纳

AJAX将数据发布到PHP文件并重定向到同一文件以回显发布数据

I have a php file with a bunch of <a></a> elements containing user names. Upon clicking one of these links, the link's data attributes are stored and posted to the second php file via AJAX. However, once I redirect to that file, the value I posted is not being displayed, instead I get the error: Undefined index userid.

Here's my code:

Index.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Post Test</title>
</head>
<body>

<a href="#" class="user-name" data-user-id="1" data-render-view="users.php">User 1</a>
<a href="#" class="user-name" data-user-id="2" data-render-view="users.php">User 2</a>
<a href="#" class="user-name" data-user-id="3" data-render-view="users.php">User 3</a>
<a href="#" class="user-name" data-user-id="4" data-render-view="users.php">User 4</a>
<a href="#" class="user-name" data-user-id="5" data-render-view="users.php">User 5</a>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$('.user-staff').on('click',function(){
  var userid = $(this).data('user-id');
  var renderView = $(this).data('render-view');

  $.ajax({
    url: "users.php",
    type: "POST",
    data: {userid:userid},
    success: function(data, textStatus, jqXHR){
      window.location = renderView;
    },
    error: function (jqXHR, textStatus, errorThrown){
      alert('Error!')
    }
  });
});

</script>
</body>
</html>

Users.php

<?php

$userIdExchanged = $_POST['userid'];

echo '<h1>user profile page</h1>';
echo $userIdExchanged; //Nothing is outputted here.

?>
  • 写回答

2条回答 默认 最新

  • doudun6928 2014-11-10 00:43
    关注

    Yes thats really what happened, basically, first step, you sent an ajax request, sending that value user-id.

    Then after that you redirect. This means that you have two separate requests.

    The first request was successful. After redirection (here comes the second) your values aren't there anymore since the data on the first request did not persist.

    I don't know why do you have to make an ajax request. Might as well submit the form normally.

    But if you really want to stay on this course, on the first request (the ajax request), you could save it in the session.

    So that value that you sent will be saved. Then after redirection, you still have it for access:

    <script type="text/javascript">
    // maybe you mean `.user-name` not `.user-staff`
    $('.user-name').on('click',function(){
    
        var userid = $(this).data('user-id');
        var renderView = $(this).data('render-view');
    
        $.ajax({
            url: "users.php",
            type: "POST",
            data: { userid: userid, setid: true }, // add a flag
            success: function(data, textStatus, jqXHR){
                window.location = renderView;
            },
            error: function (jqXHR, textStatus, errorThrown){
                alert('Error!')
            }
        });
    });
    </script>
    

    Then in PHP:

    <?php
    
    session_start();
    // if the set flag is used, set it
    if(isset($_POST['setid'])) {
        $_SESSION['userid'] = $_POST['userid'];
    }
    
    // if no flag is set, then this will just continue and echo the value currently set
    $userIdExchanged = $_SESSION['userid'];
    
    echo '<h1>user profile page</h1>';
    echo $userIdExchanged;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥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)