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.

?>

图片转代码服务由CSDN问答提供 功能建议

我有一个带有一堆&lt; a&gt;&lt; / a&gt; 包含用户名的元素。 单击其中一个链接后,链接的 data 属性将被存储并通过AJAX发布到第二个php文件。 但是,一旦我重定向到该文件,我发布的值就不会显示,而是出现错误: Undefined index userid

这是我的代码:

Index.php

 &lt;!DOCTYPE html&gt; \  n&lt; html lang =“en”&gt; 
&lt; head&gt; 
&lt; meta charset =“UTF-8”&gt; 
&lt; title&gt; Post Test&lt; / title&gt; 
&lt; / head&gt; 
&lt; 正文&gt; 
 
&lt; a href =“#”class =“user-name”data-user-id =“1”data-render-view =“users.php”&gt;用户1&lt; / a&gt; 
&lt;  ; a href =“#”class =“user-name”data-user-id =“2”data-render-view =“users.php”&gt;用户2&lt; / a&gt; 
&lt; a href =“#  “class =”user-name“data-user-id =”3“data-render-view =”users.php“&gt;用户3&lt; / a&gt; 
&lt; a href =”#“class =”user-  name“data-user-id =”4“data-render-view =”users.php“&gt; User 4&lt; / a&gt; 
&lt; a href =”#“class =”user-name“data-user-  id =“5”data-render-view =“users.php”&gt;用户5&lt; / a&gt; 
 
&lt; script src =“// ajax.googleapis.com/ajax/libs/jquery/2.1.1  /jquery.min.js"></script>
<script type =“text / javascript”&gt; 
 $('。user-staff')。on('click',function(){\  n 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; 
},
错误:函数(jqXHR,textStatus,errorThrown){
 alert('错误!')
} 
}); 
}); 
 
&lt; / script&gt  ; 
&LT; / BODY&GT; 
&LT; / HTML&GT; 
   
 
 

Users.php

 <代码>&LT ;?  php 
 
 $ userIdExchanged = $ _POST ['userid']; 
 
echo'&lt; h1&gt;用户个人资料页&lt; / h1&gt;'; 
echo $ userIdExchanged;  //这里没有输出任何内容。
 
?&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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;
    
    ?>
    
    打赏 评论
  • dongmi5020 2014-11-10 00:51

    Your class name in the html tags is "user-name" but you are listening for clicks on a class name of "user-staff" in your .js. Unsure if that is just a typo...

    <a href="#" class="user-name" ......
    $('.user-staff').on('click',function(){....
    
    打赏 评论

相关推荐 更多相似问题