Advertisement
HBiz

FIXED php checkbox array GET/POST

Jul 23rd, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.69 KB | None | 0 0
  1. //Classes.php INITIAL TABLE OF CLASS CHOICES:
  2. -------------------------------------------------------------------------------------------
  3.   while ($row = mysqli_fetch_array($data)) {
  4.  
  5.     $new_date = $row['new_date'];
  6.     $new_time = $row['new_time'];
  7.     $venue = $row['venue'];
  8.  
  9.       echo '<tr><td>' . $new_date . '</td>';
  10.       echo '<td>' . $new_time . '</td>';
  11.       echo '<td>' . $venue . '</td>';
  12.       echo '<td>' . $row['free_spaces'] . '</td>';
  13.       echo '<form action="makebooking.php" method="get">';
  14.       echo '<td><input type="checkbox" name="sesh[]" value="' . $row['class_id'] . '">';
  15.       echo '<input type="hidden" name="date[]" value="' . $new_date . '" />';
  16.       echo '<input type="hidden" name="time[]" value="' . $new_time . '" />';
  17.       echo '<input type="hidden" name="venue[]" value="' . $venue . '" /></td></tr>';
  18.    
  19.     }  
  20.     echo'</table>';
  21.  
  22.     // Make booking button
  23.       echo '<input type="submit" class="btn btn-large btn-primary pull-right" value="Book classes">';
  24.       echo '</form>';
  25. -------------------------------------------------------------------------------------------
  26. //Makebooking.php $_GET AND $_POST TO REFLECT CHOICES AND ALLOW USER TO CONFIRM BEFORE UPDATING DB WITH BOOKING
  27. -------------------------------------------------------------------------------------------
  28. <?php
  29.  
  30. //Start the session
  31. require_once('startsession.php');
  32. require_once('connectvars.php');
  33.  
  34. //---------------------------------------------------------------------------------
  35.     // Connect to the database
  36.   $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  37.  
  38. //---------------------------------------------------------------------------------
  39. //Get user's ID from the users DB and set as $username
  40.     $who = "SELECT first_name, last_name, user_id FROM users WHERE email = '" . $_SESSION['email'] . "'";
  41.     $name = mysqli_query($dbc, $who);
  42.     $row = mysqli_fetch_array($name);
  43.     $username = $row['first_name'] . ' ' . $row['last_name'];
  44.  
  45. //If GET is set, grab the data and store it in variables
  46.  
  47. if (isset($_GET['sesh'])) {
  48.    foreach ($_GET['sesh'] as $sesh) {
  49.     $class_id = $_GET['sesh']; 
  50.     $date = $_GET['date'];
  51.     $time = $_GET['time'];
  52.     $venue = $_GET['venue'];
  53. }
  54.  
  55. //---------------------------------------------------------------------------------
  56. //If POST info if all set:
  57. } else if (isset($_POST['date']) && isset($_POST['time']) && isset($_POST['venue']) && isset($_POST['username']) && isset($_POST['class_id'])) {
  58.    
  59.     foreach ($_POST['class_id'] as $classes) {
  60.         $class_id = (int)$classes;
  61.         $user_id = $_SESSION['user_id'];
  62.     }
  63. } else {
  64.         echo'<p>Oops, something went wrong. Please <a href="classes.php">go back</a> and try again.</p>';
  65.     }
  66.  
  67. //---------------------------------------------------------------------------------
  68. //If the confirm button has been hit:
  69. if (isset($_POST['submit'])) {
  70.    
  71.     //Create the foreach loop
  72.     foreach ($_POST['class_id'] as $classes) {
  73.         $class_id = (int)$classes;
  74.     //UPDATE the bookings table;
  75.          $query = "INSERT INTO bookings (user_id, booking_name, class_id, time_stamp) VALUES ('$user_id', '$username', '$class_id', NOW())";
  76.         mysqli_query($dbc, $query);
  77.     }
  78.         mysqli_close($dbc);
  79.  
  80.         // REDIRECT
  81.           $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/booking-confirmed.php';
  82.  
  83.           header('Location: ' . $home_url);
  84.  
  85.            
  86.     }
  87.  
  88. else {
  89.  
  90. //Insert the page header
  91. $page_title='Make a booking';
  92. $description='Booking your Northallerton Zumba classes.';
  93. require_once('header.php');
  94.  
  95. //html to set up the page layout
  96. ?>
  97.  
  98. <body>
  99. <?php
  100. //Insert the navigation bar
  101. require_once('navbar.php');
  102. //---------------------------------------------------------------------------------
  103.  
  104. //table header
  105. echo '<table class="table table-bordered table-hover">';
  106.   echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th><th>Add someone</th></tr></thead>';
  107. //create the form
  108. echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
  109.  
  110.  
  111. //Get the class IDs from the GET to use in the POST
  112.  
  113.     foreach ($_GET['sesh'] as $class_id) {
  114.    
  115.     $sql = "SELECT class_id, DATE_FORMAT(date, '%a, %d %b') AS new_date, DATE_FORMAT(time, '%H:%i') AS new_time, venue FROM classes WHERE class_id = '$class_id'";
  116.     $data = mysqli_query($dbc, $sql);
  117.  
  118.  
  119.  
  120. //---------------------------------------------------------------------------------
  121. //get table data
  122.     while ($row = mysqli_fetch_array($data)) {
  123.         $date = $row["new_date"];
  124.         $time = $row["new_time"];
  125.         $venue = $row["venue"];
  126.         $class_id = $row["class_id"];
  127.     }
  128.     //---------------------------------------------------------------------------------
  129. //Show a table of the selected classes
  130.  
  131.       echo '<td>' . $date . '</td>';
  132.       echo '<td>' . $time . '</td>';
  133.       echo '<td>' . $venue . '</td>';
  134.       echo '<td>' . $username . '</td>';
  135.       echo '<td><button class="btn btn-mini" type="button"><i class="icon-user"></i><i class="icon-plus"</i></button></td></tr>';
  136.       echo '<input type="hidden" name="date[]" value="' . $date . '" />';
  137.       echo '<input type="hidden" name="time[]" value="' . $time . '" />';
  138.       echo '<input type="hidden" name="venue[]" value="' . $venue. '" />';
  139.       echo '<input type="hidden" name="username[]" value="' . $username . '" />';
  140.       echo '<input type="hidden" name="class_id[]" value="' . $class_id . '" />';
  141.      
  142.     }
  143.  echo'</table>';
  144.  
  145.     //Go Back button
  146.     echo '<a class="btn btn-link pull-left" href="classes.php"><i class="icon-arrow-left"></i> Go back</a>';
  147.  
  148.     // Make booking button
  149.       echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
  150.       echo '</form>';
  151.   }
  152.  
  153. //}
  154. }
  155. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement