I want to pass a variable from one PHP file to another, for that, I am trying to use ajax. Now, in my ‘index.php’ file, I have a button. When that button is clicked, I want to pass that button’s id to another PHP file which is ‘show_schedule.php’.
Here is my index.php file’s ajax code:
$(document).on('click', '.show', function(){
var schedule_id = $(this).attr("id");
$.ajax({
url:"show_schedule.php",
method:"POST",
data:{schedule_id:schedule_id},
dataType:"json",
success:function(data)
{
window.location.href = 'show_schedule.php';
}
});
});
Here is my ‘show_schedule.php’ file:
<?php
session_start();
if(isset($_POST["schedule_id"])){
$s_id = $_POST['schedule_id'];
echo $s_id;
}
?>
data is index.php is fetched & displayed by ‘fetch.php’, in which, that button is set like this:
'<button type="button" name="show" id="'.$row["s_id"].'" class="btn btn-primary btn-sm show">Show</button>';
When I click on this button, I am redirected to ‘show_schedule.php’, but the variable value is not passed, i.e. nothing is printed on that page. Can anyone tell me where I am making mistake? Any help is appriciated.
2
Answers
You could just do the following and change the php to take the GET param i.e.
If it HAS to be a POST then a few solutions to be found @ pass post data with window.location.href
I have send data using
jQuery
ajaxpost
request togetval.php
file and data will be send successfully.ajax.php
getval.php
OUTPUT :-
The data is : test