1

In the below Javascipt code I am sending data to a page named "book.php".It is succesfully sending the data through post method(because I am getting the alert) but when I open book.php to display the recieved data,It is not showing anything.

<script type="text/javascript">
        $(document).on("click", ".open-AddBookDialog2", function (f){
            var train_date=$(this).data('id');
            $.ajax({
            type: 'post', // the method (could be GET btw)
            url: 'book.php', // The file where my php code is
            data: {
                'train_date': train_date // all variables i want to pass. In this case, only one.
            },
            success: function(data) { // in case of success get the output, i named data
                alert("success"); // do something with the output, like an alert

            }

        }); 
        });
    </script>

Book.php is showing output as nothing. Is there any way I can open the same page while sending post data to it?I want to use that $_POST variable to sql.

book.php-

<!DOCTYPE html>
<html>
<head>

 <link rel="stylesheet" type="text/css" href="style.css">
 <style>
body  {
    background-image: url("rail.jpg");
    background-color: #cccccc;
    background-size: 100% auto;
    background-repeat: no-repeat;
}
</style>
</head>
<body>

<p>
 <?php
 if(isset($_POST['train_date'])) { //if i have this post
  
     // print it  
    echo $_POST['train_date'];    }
    else{
     $var="nothing";
     echo "nothing";
    }

    
?>
</p>



</body>
</html>

2 Answers2

1

Don't use XHR/Ajax. A simple solution is to create a hidden <form method="post"> element, create likewise hidden input fields for your data (in this case just train_date) fill it with values and then trigger the form submission using javascript. The browser will then be redirected as you request.

Example:

JS:

$(document).on("click", ".open-AddBookDialog2", function (f){
    var train_date=$(this).data('id');
    var hidden_form=$("#hidden-form");
    var hidden_input=$("#hidden-form-input");
    hidden_input.val(train_date)
    hidden_form.submit()
});

HTML:

<form method="post" id="hidden-form" style="display:none" action="book.php">
    <input id="hidden-form-input" name="train_date" />
</form>
sapht
  • 2,789
  • 18
  • 16
  • I also wanted to do that but I am sending a php variable by the click of a button.That is the reason I cannot use a hidden form. – anshuman thakur Nov 11 '17 at 17:23
  • Yes you can. Dynamically fill the value of a hidden input field instead of using the data field of $.ajax. – sapht Nov 11 '17 at 19:53
0

Use ajax to send variable to php. In php stock that variable in a session (search around, is simple) then in your ajax success do a redirect: How to redirect to another webpage? . In php you will have now your var stocked in $_SESSION. Do what you need with it, manipulate the flow of execution with some if(isset()).

Botea Florin
  • 543
  • 6
  • 24
  • Np! I've done something similar trying to make some kind of browser game like Tribal Wars at the part of select owned city and display stats of it. – Botea Florin Nov 11 '17 at 18:07