0

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.

preet shah
  • 37
  • 1
  • 10
  • You can just use `this.id` rather than using the attr method. Have you inspected the call in the browser network tab to ensure the value is being passed? You can also use var_dump to view the variables on the PHP side (just output either to screen outside the isset or to a log/file. – scrappedcola Oct 13 '20 at 17:00
  • 1
    try using `console.log(schedule_id)` just after `var schedule_id = $(this).attr("id");` this line. See if `id` is showing on the console or not. – Tariqul Islam Oct 13 '20 at 17:00
  • Use the Elements panel in DevTools to see if the id is filled in correctly in the ` – Barmar Oct 13 '20 at 17:03
  • 2
    Your code is doing exactly what you wrote. See in this line `window.location.href = 'show_schedule.php';` you are redirecting into `show_schedule.php` file, so there is no point of making a `ajax` call before that and the id you passed is received on the `show_schedule.php` but after `success: (data)(...}` you are redirecting into `show_schedule.php` without any `id`. – Tariqul Islam Oct 13 '20 at 17:06
  • 2
    I would suggest if the `id` is not sensitive, then pass it within the url and receive with `GET` method. – Tariqul Islam Oct 13 '20 at 17:11
  • ok, so console.log(schedule_id) prints the id in console. The main motive is to set session variable. So I am trying to pass that variable to other file and then set session variable by PHP. so as Tariqul Islam said, after success: (data) { ... } it is redirected to show_schedule.php but without any 'id'. So what should I do? should I make another file in between to set the session variable? – preet shah Oct 13 '20 at 17:18
  • thank you everyone, I got what I was looking for. Specially thank you @TariqulIslam. – preet shah Oct 13 '20 at 17:30

2 Answers2

1

You could just do the following and change the php to take the GET param i.e.

$(document).on('click', '.show', function(){
        var schedule_id = $(this).attr("id");
        window.location.href = 'show_schedule.php?schedule_id='+schedule_id;
        });
 });

If it HAS to be a POST then a few solutions to be found @ pass post data with window.location.href

mkane
  • 880
  • 9
  • 16
  • window.location.href = 'show_schedule.php?schedule_id='+schedule_id; – preet shah Oct 13 '20 at 17:27
  • had to include '=' after schedule_id in the url, they would not let me edit your answer if the edit is less than 6 characters, so just commenting here. – preet shah Oct 13 '20 at 17:29
0

I have send data using jQuery ajax post request to getval.php file and data will be send successfully.

ajax.php

!DOCTYPE html>
<html>
<head>
    <title>ajax request</title>
    <!-- jQuery cdn -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <button id="send-data">send data</button>
    <div class="data"></div>
</body>
</html>
<script type="text/javascript"> 
    
    $(document).ready(function () {
        
        $('#send-data').click(function () {
            let val = "test";  // send val variable data
            $.post("getval.php", { value: val }, function (data, status) {  //send data using post request
                if (status == "success") {
                    $('#send-data').remove();
                    $('.data').append(data);
                }
            });
        });     
    });

</script>

getval.php

<?php

if (isset($_POST['value'])) {
    $data = $_POST['value'];
    echo "<br>The data is : ".$data;    
}

?>

OUTPUT :-

The data is : test