Php drop down menu problem

i have this school project: booking form, and it works (nearly) but when choose 2nd or more *(not first ) option in the window movie, after hiting show dates button ,(which works fine) the movie goes back to 1st one, and it destroys booking form

the link is here:
http://www.cinema.webdesignmoreno.co.uk/icinema-website/booking.php

and code

<form action="booking.php" method="post">
<?php include 'connection.php';

$sql= "SELECT DISTINCT Title FROM movies";
$result = mysqli_query($con,$sql);

echo "<select name='Title'>";
while ($row= mysqli_fetch_array($result)){
echo"<option value'" .$row['Title']."'>" .$row['Title'] ."</option>";
}
echo "</select>";


echo "<label for='GO'></label><input type='submit' name= 'go' value ='Show Dates'/>";

if(isset($_POST['go']))
{
$ShowQuery = "SELECT Date FROM movies WHERE Title='$_POST[Title]'";
mysqli_query($con, $ShowQuery);
}
$result_dates= mysqli_query($con, $ShowQuery);

echo "<select name='Date'>";
while ($row= mysqli_fetch_array($result_dates)){
echo"<option value'" .$row['Date']."'>" .$row['Date'] ."</option>";
}
echo "</select>";

$ticket=$_POST["ticket"];
echo "<input type='number' value='ticket' name='ticket'>";
echo "<input type='submit' name= 'book' value ='Book'/>";


//if the book button hit
    if(isset($_POST['book'])) 
    {
        //run sql 
$sql="SELECT Title AND Date FROM movies WHERE Title='$_POST[Title]' AND Date='$_POST[Date]'";
//if not enough stock
if (Stock < $ticket){
    echo "The tickets has been sold out";
}
//if enough stock, give details of booking
else{
    echo "<br>Your booking details:<br>
    Title: " .$_POST{Title}. "<br>
    Date: " .$_POST{Date}. "<br>
    Tickets number: " .$ticket ;
    
    //update stock
$UpdateQuery = "UPDATE Date FROM movies SET Stock=Stock - $ticket";
    }
    }
   
?>
</form>
</body>
</html>

First your submit button action code should be at the top of the page.

Use below script for movie drop down

echo "<select name='Title'>";
while ($row= mysqli_fetch_array($result))
{
      $selected = $row['Title'] == $_POST['Title'] ? ' selected="selected" ' : '';
      echo"<option value'" .$row['Title']."'   ".$selected.">" .$row['Title'] ."</option>";
}

hope this will work for you.

1 Like

brilliant
works
thanks a lot

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.