skip to Main Content

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


  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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search