分配&通过HTML表单使用PHP计算值

时间:2017-10-13 01:06:53

标签: php html

我是编程新手,我需要帮助。我在名为addbooking.php的页面上有一个HTML表单,其中包含以下代码:

<form method = "post" action = "" > 
       <table cellspacing="15">

<tr>
  <th>Bookingid </th>
  <td><input type = "text" name = "bookingid"/> </td>
</tr>

<tr>
  <th>Booking Name </th>
  <td><input type = "text" name = "bookingname" /> </td>
</tr>

<tr>
  <th> <label for = "Cabin Type">Cabin Type </label> </th>
   td><select name = "Cabin Type" id = "Cabin Type">
   <option select>Select a Cabin Type</option>
   <option value = "Luxury Cabin">Luxury Cabin </option>
   <option value = "Contemporary Cabin">Contemporary Cabin  </option>
   <option value = "Original Cabin">Original Cabin </option>
   </select>
   </td>
   </tr>

<tr>
  <th>Length of Stay </th>
  <td><input type = "text" name = "lengthofstay" /> </td>
</tr>

<tr>
  <th>Details About Guests </th>
  <td><textarea cols = "30" rows = "5" type = "text" name="guests_description" 
   /> </textarea> </td>
</tr> 

<tr>
   <th>Booking Start Date </th>
   <td><input type = "text" name = "startdate" /> </td>
</tr>

<tr>
   <th>Booking End Date </th>
   <td><input type = "text" name = "enddate" /> </td>
</tr>   

<tr>
   <th>Other Relavant Information </th>
   <td><textarea cols = "30" rows = "5" type = "text" name="other_information"  
   /> </textarea> </td>
</tr>        

<tr>
  <th> </th>
  <td> <input type = "submit" value = "Create Booking!" name = 
       "bookingdetails" /> </td>
</tr> 
</table>             
</form>

我在同一页面上也有php代码,如下所示:

<?php                         
   include ('dbconnect.php');  

// get values from the form
     function getPosts(){
     $posts = array();
     $posts[0] = $_POST['bookingid'];
     $posts[1] = $_POST['bookingname'];
     $posts[2] = $_POST['lengthofstay'];
     $posts[3] = $_POST['guests_description'];       
     $posts[4] = $_POST['startdate'];
     $posts[5] = $_POST['enddate'];
     $posts[6] = $_POST['other_information'];
     return $posts;
    } 

 if (isset($_POST['bookingdetails'])) {  

  $data = getPosts();


 if (empty($bookingname)){
        $errors[] = "Please enter the your booking name.";
    }

 if(empty($errors)){
        foreach($errors as $error){
            echo $error . "<br/>";
    }
 else{

  $insert_Query = "insert into bookings 
    (Bookingid,Bookingname,Lengthofstay,Details_about_guests,
     Booking_start_date,Booking_end_date,Other_relavant_information)
     values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]',
     '$data[5]','$data[6]')";

    $result = mysqli_query($db, $insert_Query);

 if ($result)
 {
 echo "<font color = 'green' . <p>Successfully Booked</p> </font>";
 }
 else{
   echo "<p> Something went Wrong </p>" . mysqli_error ($db);

    }
   }
  }
  ?>

我的数据库中也有一个名为cabins的表格,如下所示:

Picture of the Cabins table

现在,我需要一些帮助,如何将Cabin_Price列中的每个价格分配到表单下拉菜单中的每个项目,然后在&#34;预订详细信息&#34;按下按钮乘以所选的值/选项,例如1200&#39;在“逗留时间长度”中输入的价值是多少?例如,&#39; 4&#39;用php。下面是我的意思:

Assigning the Values to Each Cabin Type

1 个答案:

答案 0 :(得分:0)

更改菜单,以便选项的值为客舱ID。然后,当您处理表单时,您查看价格,并乘以停留时间。因此菜单应如下所示:

<select name = "Cabin Type" id = "Cabin Type">
    <option select>Select a Cabin Type</option>
    <option value = "1">Luxury Cabin </option>
    <option value = "2">Contemporary Cabin  </option>
    <option value = "3">Original Cabin </option>
</select>

提交表单时,您会执行以下查询:

$stmt = $dbh->prepare("SELECT cabin_price FROM cabins WHERE cabin_id = :id");
$stmt->bindParam(":id", $_POST["Cabin Type"]);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$price = $row['cabin_price'];
$total_price = $price * $_POST['lengthofstay'];