As new to php i am not clear over the jumps betveen php-pages. My code below. is this the right way?

Viewed 37
  1. I want to se available dates in a dropdown - done
  2. After i´v choosen the date i want to see the available time - done
  3. The customer should be able to book the choosen timeslot - I think i knowe this one, work in progress.

Problem: I want the customer to se the dropdown and the result in the same page and maybe change it when the time comes visably. I´v been playing around with action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" but thats not it i belive.

I am a bit lost in best practice regarding files and jumps betveen them. I have separated the php and html code in different pages. When i´m ready i lift the php-files to includes folder offcourse.

HTML - form with dropdown and submit php - fill dropdown with dates html - form with result text fields php - code for result (timeslot data)

form_date.php

<?PHP
  require_once('booking_available_dates.php');
?>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<form name='date_registration' action='booking_available_dates.php' method='POST'>
   
  <div class="form-group row">
    <label for="select" class="col-2 col-form-label">Hämta första lediga tid för valt datum</label> 
    <div class="col-10">
      <select id="date_input" name="date_input" class="custom-select">
      <?php foreach ($dates as $row): ?>
      <option><?=$row["date_booking_customer"]?></option>
      <?php endforeach ?>
      </select>
    </div>
  </div> 

  <div class="form-group row">
    <div class="offset-4 col-8">
      <button name="submit" type="submit" class="btn btn-primary">Hämta tid!</button>
    </div>
  </div>
</form>

booking_available_dates.php

<?php
require_once('dbhandler.php');
$dbh = new DBHandler();

if ($dbh->getInstance() === null) {
    die("No database connection");
}

try {
    $sql = "CALL `booking_dates_available`();";
    $stmt = $dbh->getInstance()->prepare($sql);
    $stmt->execute();
    $dates=$stmt->fetchAll();
} 

catch(PDOException $e) {
    echo ($e->getMessage());
}
?>

booking_confirm_view.php (not ready - work in progress)

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<form>
  <div class="form-group row">
    <label for="date" class="col-4 col-form-label">Datum</label> 
    <div class="col-8">
      <input id="date" readonly="readonly" name="date" type="text" class="form-control">
    </div>
  </div>
  <div class="form-group row">
    <label for="time" class="col-4 col-form-label">Tid</label> 
    <div class="col-8">
      <input id="time" readonly="readonly" name="time" type="text" class="form-control">
    </div>
  </div>
  <div class="form-group row">
    <label for="owner" class="col-4 col-form-label">Båtägare</label> 
    <div class="col-8">
      <input id="owner" name="owner" type="text" class="form-control" required="required">
    </div>
  </div>
  <div class="form-group row">
    <label class="col-4">Medlem i HSS?</label> 
    <div class="col-8">
      <div class="custom-control custom-radio custom-control-inline">
        <input name="member" id="member_0" type="radio" required="required" class="custom-control-input" value="thru"> 
        <label for="member_0" class="custom-control-label">Medlem</label>
      </div>
      <div class="custom-control custom-radio custom-control-inline">
        <input name="member" id="member_1" type="radio" required="required" class="custom-control-input" value="false"> 
        <label for="member_1" class="custom-control-label">Ej medlem</label>
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="boatname" class="col-4 col-form-label">Båtnamn</label> 
    <div class="col-8">
      <input id="boatname" name="boatname" type="text" class="form-control">
    </div>
  </div>
  <div class="form-group row">
    <label for="email" class="col-4 col-form-label">E-post</label> 
    <div class="col-8">
      <input id="email" name="email" type="text" class="form-control" required="required">
    </div>
  </div>
  <div class="form-group row">
    <label for="mobile" class="col-4 col-form-label">Mobilnummer</label> 
    <div class="col-8">
      <input id="mobile" name="mobile" type="text" class="form-control" required="required">
    </div>
  </div> 
  <div class="form-group row">
    <div class="offset-4 col-8">
      <button name="submit" type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

booking_available_time.php

<?php
require_once('dbhandler.php');
$dbh = new DBHandler();

if ($dbh->getInstance() === null) {
    die("No database connection");
}

try {
    $sql = "CALL `booking_date_input` (:date_input);";
    $stmt = $dbh->getInstance()->prepare($sql);
    $stmt->bindParam(':date_input', $date_input, PDO::PARAM_STR);
    $date_input = $_POST["date_input"];
    $stmt->execute();
    $result=$stmt->fetchAll();
} 

catch(PDOException $e) {
    echo ($e->getMessage());
}
?>

$result is the array with the timeslot data.

0 Answers
Related