0

I am appending divisions to my html page dynamically using jquery append method. This division consists of a sub division. I want to redirect to another page with some information using sessions when the div is clicked. But the page is being redirected to my target page without even clicking on it.

<script>
    $('#economy').append('<div id="element">'+
                            '<img src=<?php echo $row['photo'] ?> class="bike-img">'+
                            '<p class="bike-desc" style="font-size:18px;"><?php echo $row['name'] ?></p>'+
                            '<?php if(in_array($row['name'], $bikes)) { ?>'+
                            '<div id= <?php echo $row['name']?> class="availability notavailable"><p class="bike-desc">Already Booked</p></div>'+
                            '<?php } else {?>'+
                            '<div id= <?php echo $row['name']?> class="availability available" onclick="book(this.id)"><p class="bike-desc">Book Now</p></div>'+
                            '<?php } ?>'+
                            '</div>');
    </script>

The function after onclick:

<script>
function book(id){
document.cookie = "bike = " + id;
<?php
    $_SESSION['from']=$_GET['from'];
    $_SESSION['to']=$_GET['to'];
    $_SESSION['pickup']=$_GET['pickup'];
    $_SESSION['bike']=$_COOKIE['bike'];
    ---header("Location:bikebooking.php");---   
?>
window.location.href = "bikebooking.php";
});
</script>

I even tried using jquery but I am facing the same problem my main page is automatically redirecting to the direct page without any clicks.

  • 2
    you're mixing PHP and javascript, that won't work this way. PHP is parsed on the server so you get the page back and it immediatly does the header()-command to redirect. – jonas3344 Mar 15 '18 at 13:14
  • You'd have to do the redirect via javascript (not as a php header). like `window.location.href = bikebooking.php;` – Jeff Mar 15 '18 at 13:17
  • But my book function is not being triggered. What can I do to make it work? – sai chaitanya Mar 15 '18 at 13:30

0 Answers0