d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Classes And Sub Classes In Php Ood
Add Reply New Topic New Poll
Member
Posts: 10,801
Joined: Apr 5 2010
Gold: 20.00
Nov 10 2016 05:23pm
im a little lost right now, when yo go to this page, you have 3 options in a drop down generic, tool and electronic. when you hit submit a forum pops up depending on your choice you fill it out and it is saved to da database. now he told us we would never create a webpage like this but for education purposes we are. here my start page code like 90 ish lines pretty easy to read and understand. now even after he hit submit the forum apears bwlow with a 2nd submit choice, again only cause its the way he wanted us to do it.

Code
[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log product</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h3>Log Product exercise</h3>

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>what are you adding</legend>
<select id="product_type" name="product_type">
<option value="Generic" >Generic</option>
<option value="Tool" >Tool</option>
<option value="Electronic" >Electronic</option>
</select><br />
<input type="submit" value="submit" name="submit" />
</form>


<?php

$Generic = $_POST['Generic'];
$Tools = $_POST['Tools'];
$Electronic = $_POST['Electronic'];

if (isset($_POST['submit'])) {
if(isset($_POST['product_type'])) {
if($_POST['product_type'] == 'Generic') {
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>what type of generic product are you adding?</legend>
<label for="title">title of product:</label>
<input type="text" id="title" name="title" /><br />
<textarea id="description" name="description" rows="8" cols="40"></textarea><br />
<label for="price">what is the price of your product:</label>
<input type="text" id="price" name="price" /><br />
<input type="submit" value="submit" name="submit" />
</form>

<?php
if (isset($_POST['submit'])) {
require_once('Product_class.php');
$Product = new Product_class();
echo('hey');
//variable list
$Product->settitle($_POST['title']);
$Product->setdescription($_POST['description']);
$Product->setprice($_POST['price']);
$Product->setInsert();
}
?>

<?php
} else if($_POST['product_type'] == 'Tool') {
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>what services can ship this item</legend>
<select id="Tools" name="Tools">
<option value="UPS" >generic</option>
<option value="FedEx" >Tool</option>
<option value="USPS" >Electronic</option>
</select><br />
<label for="price">what is the price of the tool:</label>
<input type="text" id="price" name="price" /><br />
<input type="submit" value="submit" name="submit" />
</form>

<?php
} else if($_POST['Recycleable'] == 'Recycleable') {
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>is your electronic recycleable</legend>
<select id="Recycleable" name="Recycleable">
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select><br />
<input type="submit" value="submit" name="submit" />
</form>
<?php
}
}
}
?>

</body>
</html>

[/CODE]

now what i dont get is how you put a class like this into the html. for 1 thing when the user hit submit first it executed the 2nd submit no idea how to stop that and even then it says "( ! ) Fatal error: Call to undefined method Product_class::settitle() in /home/ubuntu/workspace/week 11/addProduct.php on line 51" ?????i thought i defined it in the class errrr help

Code
<?php
class Product_class
{
private $title;
private $description;
private $price;

private $Insert;

// Getters
public function gettitle()
{
return $this->title;
}

public function getdescription()
{
return $this->description;
}

public function getprices()
{
return $this->prices;
}


// Setters
public function setnoun($title)
{
$this->title = $title;
}

public function setverb($description)
{
$this->description = $description;
}

public function setadjective($price)
{
$this->price = $price;
}

//insert function
public function setInsert()
{
echo('cool');
if(empty($this->gettitle()) || empty($this->getdescription()) || empty($this->getprice()) || empty($this->getshipper()) || empty($this->getweight()) || empty($this->getRecycleable())) {
echo "you did not fill out a certain field<br/>";
} else {

//connecting to the db
$dbc = mysqli_connect('localhost', 'root', '', 'madlib')
or die('Error connecting to MySQL server.');

//inserting form info into db
$query = "INSERT INTO product (title, description, price, shipper, weight, electronic) " .
"VALUES ('" . $this->gettitle() . "', '" . $this->getdescription() . "', '" . $this->getprice() . "', '" . $this->getshipper() . "' , '" . $this->getweight() . "' , '" . $this->getRecycleable() . "')";

echo($query);
//get query result
$result = mysqli_query($dbc, $query)
or die('Error querying database.');

//close connection
mysqli_close($dbc);
}
}

}
?>


the other 2 sub classes

Code
<?php
require_once('Product.php');
class Tools extends Product
{

private $shipper;
private $weight;

// Getters
public function getshipper()
{
return $this->shipper;
}

public function getweight()
{
return $this->weight;
}


// Setters
public function setnoun($shipper)
{
$this->shipper = $shipper;
}

public function setverb($weight)
{
$this->weight = $weight;
}
}
?>


Code
<?php
require_once('Product.php');
class Recycleable extends Product
{
private $Recycleable;

// Getters

public function getRecycleable()
{
return $this->recyclable;
}

// Setters
public function setRecycleable($Recycleable)
{
$this->Recycleable = $Recycleable;
}


}
?>


This post was edited by jsbb on Nov 10 2016 05:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 10 2016 09:55pm
Quote
now what i dont get is how you put a class like this into the html. for 1 thing when the user hit submit first it executed the 2nd submit no idea how to stop that and even then it says "( ! ) Fatal error: Call to undefined method Product_class::settitle() in /home/ubuntu/workspace/week 11/addProduct.php on line 51" ?????i thought i defined it in the class errrr help


can you point out which line you defined it in? i don't see it in your Product_class
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 11 2016 07:02pm
Quote (jsbb @ Nov 11 2016 01:01pm)
dealing with 2 submit forums like this how do you prevent the second forum from being submitted after clicking submit on the first forum. i tryed changing the name to submit_generic same with on post for the second forum, that completely breaks it. imm not sure how to proceed

i fixed it all the way up the the point of it reaching the insert class on the product_class file "heycoolyou did not fill out a certain field" again i get that message cause it submit the 2nd forums after submitting the 1st forum
Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log product</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h3>Log Product exercise</h3>

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>what are you adding</legend>
<select id="product_type" name="product_type">
<option value="Generic" >Generic</option>
<option value="Tool" >Tool</option>
<option value="Electronic" >Electronic</option>
</select><br />
<input type="submit" value="submit choice" name="submit" />
</form>


<?php

$Generic = $_POST['Generic'];
$Tools = $_POST['Tools'];
$Electronic = $_POST['Electronic'];

if (isset($_POST['submit'])) {
if(isset($_POST['product_type'])) {
if($_POST['product_type'] == 'Generic') {
?>

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>what type of generic product are you adding?</legend>
<label for="title">title of product:</label>
<input type="text" id="title" name="title" /><br />
<textarea id="description" name="description" rows="8" cols="40"></textarea><br />
<label for="price">what is the price of your product:</label>
<input type="text" id="price" name="price" /><br />
<input type="submit" value="submit product" name="submit" />
</form>

<?php
if (isset($_POST['submit'])) {
require_once('Product_class.php');
$Product = new Product_class();
echo('hey');
//variable list
$Product->settitle($_POST['title']);
$Product->setdescription($_POST['description']);
$Product->setprice($_POST['price']);
$Product->setInsert();
}
?>

<?php
} else if($_POST['product_type'] == 'Tool') {
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>what services can ship this item</legend>
<select id="Tools" name="Tools">
<option value="UPS" >generic</option>
<option value="FedEx" >Tool</option>
<option value="USPS" >Electronic</option>
</select><br />
<label for="price">what is the price of the tool:</label>
<input type="text" id="price" name="price" /><br />
<input type="submit" value="submit" name="submit" />
</form>

<?php
} else if($_POST['Recycleable'] == 'Recycleable') {
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<legend>is your electronic recycleable</legend>
<select id="Recycleable" name="Recycleable">
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select><br />
<input type="submit" value="submit" name="submit" />
</form>
<?php
}
}
}
?>

</body>
</html>



Do not pm me.

http://stackoverflow.com/questions/23297605/how-to-separate-two-php-form-submit-functions
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Jan 23 2017 11:37am
Are you asking how to directly instantiate Product_Class.php in the HTML? Ie: <div on-click="callMyphpfunctionshere???"></div>
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll