Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('header.php'); // include header file
- require_once('connectvars.php'); // include connections to database
- ?>
- <h2>Input Listing</h2>
- <?php
- $output_form = true; // declares a flag to test whether or not to show the form
- $address = NULL;
- $price = NULL;
- $bedroom = NULL;
- $bath = NULL;
- $sqft = NULL;
- $year = NULL;
- $pool = 0; // initialization for checkbox
- $basement = 0; // initialization for checkbox
- $fence = 0; // initialization for checkbox
- $featured = NULL;
- if ( isset($_POST['submit']) ) {
- // condition based on whether or not the user submits form
- // php variable assignments to names used in HTML file
- $address = $_POST['address']; // street address
- $price = $_POST['price']; // price
- $bedroom = $_POST['bedroom']; // number of bedrooms
- $bath = $_POST['bath']; // number of baths
- $sqft = $_POST['sqft']; // sq. ft.
- $year = $_POST['year']; // year built
- if (isset($_POST['pool'])) {
- $pool = $_POST['pool']; // pool
- }
- if (isset($_POST['basement'])) {
- $basement = $_POST['basement']; // finished basement
- }
- if (isset($_POST['fence'])) {
- $fence = $_POST['fence']; // fenced in yard
- }
- if (isset($_POST['featured'])) {
- $featured = $_POST['featured']; // featured listing
- } else {
- $featured = NULL;
- }
- $output_form = false; // will only change to TRUE based on validation.
- // Validation of inputs, minimum reqs: street address and price.
- if ( empty($address) ) {
- echo "You must input the listing's Street Address.<br />"; // Validation to include street address.
- $output_form = true; // will print form
- }
- if ( empty($price) ) {
- echo "You must input the listing's price.<br />"; // Validation to include the listing's price.
- $output_form = true; // will print form
- }
- if ( !is_numeric($price) ) {
- echo "Listing price must be a numeric value.<br />"; // Validation to make numeric.
- $output_form = true; // will print form
- }
- // Once everything in form is correct, run
- if ( !empty($address) && !empty($price) && is_numeric($price) ) {
- // Connect to database
- $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
- or die('Error connecting to MySQL server.'); // terminates code if failed.
- // creates the SQL query and stores as string in a PHP variable
- $query = "INSERT INTO details (address, price, bedroom, bath, sqft, year, pool, basement, fence, featured) " .
- "VALUES ('$address', '$price', '$bedroom', '$bath', '$sqft', '$year', '$pool', '$basement', '$fence', '$featured')";
- // issues the query using the msqli_query function that allows it to add data to the database table
- $result = mysqli_query($dbc, $query)
- or die('Error querying database.'); // terminates if failed
- mysqli_close($dbc); // close database connection
- // Output for data submitted, verifies that information has been received and processed by system.
- echo '<h3>Your listing has been input into the system.</h3><br/>';
- } // end of if statement that allows run if form is correct
- } // end of isset condition that was based on whether or not the form was submitted
- if($output_form) { // only show the form if user has error or not submitted.
- ?>
- <!-- load form and use self-referencing PHP variable. -->
- <br />
- <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
- <label for="address">Street Address:</label>
- <input type="text" id="address" name="address" value="<?php echo $address; ?>" /><br />
- <label for="price">Price:</label>
- <input type="text" id="price" name="price" value="<?php echo $price; ?>" /><br />
- <label for="bedroom">Number of Bedrooms:</label>
- <input type="text" id="bedroom" name="bedroom" value="<?php echo $bedroom; ?>" /><br />
- <label for="bath">Number of Bathrooms:</label>
- <input type="text" id="bath" name="bath" value="<?php echo $bath; ?>" /><br />
- <label for="sqft">Sq. Footage:</label>
- <input type="text" id="sqft" name="sqft" value="<?php echo $sqft; ?>" /><br />
- <label for="year">Year Built:</label>
- <input type="text" id="year" name="year" value="<?php echo $year; ?>" /><br />
- <label>Extras:</label>
- <input type="checkbox" name="pool" style="width: auto" value="1" <?php if ($pool) echo "checked"; ?> > Pool
- <input type="checkbox" name="basement" style="width: auto" value="1" <?php if ($basement) echo "checked"; ?> > Finished Basement
- <input type="checkbox" name="fence" style="width: auto" value="1" <?php if ($fence) echo "checked"; ?> > Fenced-in Yard<br />
- <label for="featured">Featured:</label>
- <input type="radio" name="featured" value="yes" <?php if ($featured == "yes") echo "checked"; ?> > Yes
- <input type="radio" name="featured" value="no" <?php if ($featured == "no") echo "checked"; ?> > No <br />
- <input type="submit" value="Submit" name="submit" />
- </form>
- <?php } // closes the condition related to output_Form, before loading form
- require_once('footer.php');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement