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.
?>