Having issues with displaying my estimated cost

43 Views Asked by At
<?php
session_start();
$title = "Cost Estimate";
include 'header.php';

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Services";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['service'])) {
    $selected_service = $_POST['service'];

    if ($selected_service == "Tile & Grout Cleaning") {
        $area = $_POST['area'];
        $cost = 0;

        // Fetch price from database
        $sql = "SELECT price FROM Services WHERE service_name = 'Tile & Grout Cleaning'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            $price = $row["price"];

            $cost = $price * $area;

            echo "<h2>Cost Estimate for $selected_service</h2>";
            echo "<p>Area: $area square feet</p>";
            echo "<p>Estimated Cost: $$cost</p>";
        } else {
            echo "<p>No records found. Please select a service.</p>";
        }

    }

}

$conn->close();

include 'footer.php';
?>

<?php
session_start();
$title = "Estimate";
include 'header.php';
?>

<h2>Estimate</h2>
<center>

  <h2>Service Estimate Form</h2>
  <form action="estimate.php" method="post">
    <label for="service">Select a service:</label>
    <select class="service" name="service">
      <option value="">--Select--</option>
      <option value="Tile & Grout Cleaning">Tile & Grout Cleaning</option>
      <option value="Wooden Floor Polishing">Wooden Floor Polishing</option>
    </select>
    <br><br>
    <input type="submit" class="estimate">
  </form>
  <br /><br />
  <?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $selected_service = $_POST['service'];

    if ($selected_service == "Tile & Grout Cleaning") {
      echo '<h2>Tile & Grout Cleaning</h2>';
      echo '<form action="cost.php" method="post">';

      echo '<label for="area">How large is the area to be cleaned (in square feet)?: </label>';
      echo '<input type="number" name="area" required><br><br>';
      echo '<input type="submit" value="Calculate Cost">';
      echo '</form>';
    } else {
      echo '<p>Please select a service.</p>';
    }
  } ?>

  <?php include 'footer.php'; ?>

When I go to submit the details that are needed to estimate a total the cost page is blank. This has me stumped. I felt like I tried everything. Even went on chatgtp hoping for a solution but it told me to change the action estimate,php to cost,php but I need the action estimate,php to get the specifications of the service.

The output that should be shown is (random values added for output)

-Cost Estimate for Tile and Grout Cleaning

-Area: 100 square feet

-Estimated Cost: $90

I know I just posted everything but I'm new so I'm not sure how to properly use the features.

Help is appreciated.

1

There are 1 best solutions below

8
Mohammed Jhosawa On

Code adds a hidden input field to transfer the selected service from a form submission page to another page called cost.php.

<?php
session_start();
$title = "Estimate";
include 'header.php';
?>

<h2>Estimate</h2>
    <center>
    <h2>Service Estimate Form</h2>
    <form method="post" action="estimate.php">
        <label for="service">Select a service:</label>
        <select class="service" name="service">
            <option value="">--Select--</option>
            <option value="Tile & Grout Cleaning">Tile & Grout Cleaning</option>
            <option value="Wooden Floor Polishing">Wooden Floor Polishing</option>
        </select>
        <br><br>
        <input type="submit" class="estimate">
    </form>
<br />
<br />
<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $selected_service = $_POST['service'];
    if ($selected_service == "Tile & Grout Cleaning") {
        echo '<h2>Tile & Grout Cleaning</h2>';
        echo '<form action="cost.php" method="post">';
        echo '<label for="area">How large is the area to be cleaned (in square feet)?: </label>';
        echo '<input type="number" name="area" required><br><br>';
        // Added hidden value to make sure service value is passed to cost.php in $_POST arguments
        echo '<input type="hidden" name="service" value="' . $selected_service . '">';
        echo '<input type="submit" value="Calculate Cost">';
        echo '</form>';
    } else {
        echo '<p>Please select a service.</p>';
    }
}

?>
    
<?php
include 'footer.php';
?>