2

I am doing some practicing with OOP in PHP, and am having issues with submitting form data involving subclasses.

What I am trying to do: submit form data based on the type of product it is (generic, tool, or electronic). My concern comes from not being able to submit a form that can differentiate between the different product types.

Here's the Product Class (the base class):

<?php
require_once('connectvars.php');

// Base class!!
class Product {
    // Inheritable properties
    protected $title;
    protected $description;
    protected $price;

    // Getters
    public function getTitle() {
        return $this->title;
    }
    public function getDescription() {
        return $this->description;
    }
    public function getPrice() {
        return $this->price;
    }

    // Setters
    public function setTitle($title) {
        $this->title = $title;
    }
    public function setDescription($description) {
        $this->description = $description;
    }
    public function setPrice($price) {
        $this->price = $price;
    }

    public function insertProduct() {
        $dbc    = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
        $query  = "INSERT INTO addedProducts VALUES (0,'$this->title', '$this->description', '$this->price', '', '', '')";

        mysqli_query($dbc, $query)
            or die("Error adding to database");

        mysqli_close($dbc);
    }
}
?>

Here's a subclass I made called Tools:

<?php
require_once('connectvars.php');
require_once('Product.php');

class Tools extends Product {
    // Defined properties specific to Tools class
    private $shipper;
    private $weight;

    // Getters
    public function getShipper() {
        return $this->shipper;
    }
    public function getWeight() {
        return $this->weight;
    }

    // Setters
    public function setShipper($shipper) {
        $this->shipper = $shipper;
    }
    public function setWeight($weight) {
        $this->weight = $weight;
    }

    public function insertTool() {
        $dbc    = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
        $query  = "INSERT INTO addedProducts VALUES (0,'$this->title', '$this->description', '$this->price', '$this->shipper', '$this->weight', '')";

        mysqli_query($dbc, $query)
            or die("Error adding to database");

        mysqli_close($dbc);
    }
}
?>

This is where I am running into problems:

<!DOCTYPE html>
<html>
<head>
   <title>Product Entry</title>
</head>
<body>
    <select name="prodType" id="prodType">
        <option value="" selected="selected">Select...</option>
        <option value="general">General</option>
        <option value="tools">Tools</option>
        <option value="electronics">Electronics</option>
    </select>
    <br/><br/>
<?php
//require_once('connectvars.php');
require_once('Product.php');
require_once('Electronics.php');
require_once('Tools.php');

$product    = new Product();
$tool       = new Tools();
$electronic = new Electronics();

if (isset($_POST['submit']) && (isset($_POST['prodType']) == 'general')) {
    $product_form = false;

    $product->setTitle($_POST['title']);
    $product->setDescription($_POST['description']);
    $product->setPrice($_POST['price']);             

    $product->insertProduct();

    /*$tool->setTitle($_POST['title']);
    $tool->setDescription($_POST['description']);
    $tool->setPrice($_POST['price']);
    $tool->setShipper($_POST['shipper']);
    $tool->setWeight($_POST['weight']);
    if (!empty($tool->getTitle()) && !empty($tool->getDescription()) &&     is_numeric($tool->getPrice()) && !empty($tool->getShipper()) && !empty($tool-    >getWeight())) {
        echo 'Tool submitted <br/>';
        //echo '<a href="addProduct.php">Go Back</a>';
        $tool->insertTool();
    }
} else {
    $product_form = true;
}

if ($product_form) {
?>
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <label for="title"><strong>Product Title</strong></label>
        <br/>
        <input type="text" id="title" name="title" value="<?php echo $product->getTitle();?>"/>
        <br/><br/>
        <label for="description"><strong>Description</strong></label>
        <br/>
        <input type="text" id="description" name="description" value="<?php echo $product->getDescription();?>"/>
        <br/><br/>
        <label for="price"><strong>Price</strong></label>
        <br/>
        <input type="text" id="price" name="price" value="<?php echo $product->getPrice();?>"/>
        <br/><br/>            
         <!--For Tools -->
        <label for="shipper"><strong>Shipper Info</strong></label>
        <br/>
        <select name="shipper" id="shipper">
            <option value="none" selected="selected">--</option>
            <option value="usps">USPS</option>
            <option value="fedex">FedEx</option>
            <option value="ups">UPS</option>
        </select>
        <br/><br/>
        <label for="weight"><strong>Weight</strong></label>
        <br/>
        <input type="text" id="weight" name="weight" value="<?php echo $tool->getWeight();?>"/>
        <br/><br/>            
         <!--For Electronics -->
        <label for="recyclable"><strong>Recyclable?</strong></label>
        <br/>
        <select name="recyclable" id="recyclable">
            <option value="none" selected="selected">--</option>
            <option value="yes">Yes</option>
            <option value="no">No</option>
        </select>            
        <br/><br/>
        <input type="submit" id="submit" name="submit" value="Submit Product"/>
    </form>
<?php
}
?>
</body>
</html>

I'm sure there's a fairly straightforward solution, but I'm no longer thinking about this correctly anymore -_-. Any suggestions?

4
  • 2
    What is the actual behavior? Is there any error message? Commented Nov 17, 2015 at 4:50
  • @FelisCatus, there is no error message--when I select 'general' and press submit, the form clears out but nothing is sent to the database Commented Nov 17, 2015 at 4:57
  • Put your prodType select option into inside the form. Thia may be overcomes your problem Commented Nov 17, 2015 at 4:59
  • Where do you select 'general'. Am i missing a select box? Commented Nov 17, 2015 at 12:11

1 Answer 1

2

I would do the following:

  1. Move all of your calculations to the top of the file.
  2. Move your prodType into the form.
  3. I am displaying the form always. In 1 instance it is to edit, in another it is to create. But you will want to add a hidden input for the "product_id"

Like this:

<?php
require_once('Product.php');
require_once('Electronics.php');
require_once('Tools.php');

$product    = new Product();
$tool       = new Tools();
$electronic = new Electronics();
if (isset($_POST['submit'])){
     $prodType = $_POST['prodType'];
     if($prodType == 'general') {
        $product_form = false;
        $product->setTitle($_POST['title']);
        $product->setDescription($_POST['description']);
        $product->setPrice($_POST['price']);             
        $product->insertProduct();
    } else if($prodType == 'tools') {

    } else if ($prodType == 'elecronics') {
    } else {
        // echo this message in the form. 
        $msg = 'Invalid product type';
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Product Entry</title>
</head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <select name="prodType" id="prodType">
        <option value="" selected="selected">Select...</option>
        <option value="general">General</option>
        <option value="tools">Tools</option>
        <option value="electronics">Electronics</option>
    </select>
    <br/><br/>
    <label for="title"><strong>Product Title</strong></label>
    <br/>
    <input type="text" id="title" name="title" value="<?php echo $product->getTitle();?>"/>
    <br/><br/>
    <label for="description"><strong>Description</strong></label>
    <br/>
    <input type="text" id="description" name="description" value="<?php echo $product->getDescription();?>"/>
    <br/><br/>
    <label for="price"><strong>Price</strong></label>
    <br/>
    <input type="text" id="price" name="price" value="<?php echo $product->getPrice();?>"/>
    <br/><br/>            
    <!--For Tools -->
    <label for="shipper"><strong>Shipper Info</strong></label>
    <br/>
    <select name="shipper" id="shipper">
        <option value="none" selected="selected">--</option>
        <option value="usps">USPS</option>
        <option value="fedex">FedEx</option>
        <option value="ups">UPS</option>
    </select>
    <br/><br/>
    <label for="weight"><strong>Weight</strong></label>
    <br/>
    <input type="text" id="weight" name="weight" value="<?php echo $tool->getWeight();?>"/>
    <br/><br/>            
    <!--For Electronics -->
    <label for="recyclable"><strong>Recyclable?</strong></label>
    <br/>
    <select name="recyclable" id="recyclable">
        <option value="none" selected="selected">--</option>
        <option value="yes">Yes</option>
        <option value="no">No</option>
    </select>            
    <br/><br/>
    <input type="submit" id="submit" name="submit" value="Submit Product"/>
</form>
</body>
</html>

Note: You should use and learn composer. It is a must have tool to autoload your class files.

Sign up to request clarification or add additional context in comments.

2 Comments

I can get it to submit a 'general' product properly, but cannot submit a 'tools' product. This is the kind of validation I am attempting to do: if (isset($_POST['submit']) && (isset($_POST['prodType']) == 'tools')) { //$tool->insertTool(); } Note: I am attempting to do this after I created the validation (like you suggested above) for submitting a general product
Thanks! I've been completely 'brain-farting' the past few days, and this is exactly what I was looking to do!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.