php - How do I change description name, if a different category is chosen?
Get the solution ↓↓↓I am trying to make a small webstore project, for the pagecreate_product.php
I am having it display the name, price and the description(which is three sub-categories - size, weight, and length), and then you can choose the category, for example I choose the category - book, I want the description to change to weight, if I change it to electronics i want it to change to size and so on.
Here is my create_product.php
<?php
// include database and object files
include_once 'config/database.php';
include_once 'objects/product.php';
include_once 'objects/category.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
// pass connection to objects
$product = new Product($db);
$category = new Category($db);
// set page headers
$page_title = "Create Product";
include_once "layout_header.php";
echo "<div class='right-button-margin'>";
echo "<a href='index.php' class='btn btn-default pull-right'>Read Products</a>";
echo "</div>";
?>
<?php
// if the form was submitted - PHP OOP CRUD Tutorial
if($_POST){
// set product property values
$product->name = $_POST['name'];
$product->price = $_POST['price'];
$product->description = $_POST['description'];
$product->category_id = $_POST['category_id'];
// create the product
if($product->create()){
echo "<div class='alert alert-success'>Product was created.</div>";
}
// if unable to create the product, tell the user
else{
echo "<div class='alert alert-danger'>Unable to create product.</div>";
}
}
?>
<!-- HTML form for creating a product -->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>Name</td>
<td><input type='text' name='name' class='form-control' /></td>
</tr>
<tr>
<td>Price</td>
<td><input type='text' name='price' class='form-control' /></td>
</tr>
<tr>
<?php
if($id==1):
?>
<td>Size</td>
<td><textarea name='description' class='form-control'></textarea></td>
<?php
else:
?>
<td>Weight</td>
<td><textarea name='description' class='form-control'></textarea></td>
<?php
endif
?>
</tr>
<tr>
<td>Category</td>
<td>
<?php
// read the product categories from the database
$stmt = $category->read();
// put them in a select drop-down
echo "<select class='form-control' name='category_id'>";
echo "<option>Select category...</option>";
while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row_category);
echo "<option value='{$id}'>{$name}</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" class="btn btn-primary">Create</button>
</td>
</tr>
</table>
</form>
<?php
// footer
include_once "layout_footer.php";
?>
In the form I am trying to do like an if statement where if the category value is 1 the description changes to size
<!-- HTML form for creating a product -->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>Name</td>
<td><input type='text' name='name' class='form-control' /></td>
</tr>
<tr>
<td>Price</td>
<td><input type='text' name='price' class='form-control' /></td>
</tr>
<tr>
<?php
if($id==1):
?>
<td>Size</td>
<td><textarea name='description' class='form-control'></textarea></td>
<?php
else:
?>
<td>Weight</td>
<td><textarea name='description' class='form-control'></textarea></td>
<?php
endif
?>
</tr>
<tr>
<td>Category</td>
<td>
<?php
// read the product categories from the database
$stmt = $category->read();
// put them in a select drop-down
echo "<select class='form-control' name='category_id'>";
echo "<option>Select category...</option>";
while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row_category);
echo "<option value='{$id}'>{$name}</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" class="btn btn-primary">Create</button>
</td>
</tr>
</table>
</form>
<?php
WHat should I write so that the description changes with the change of category?
Answer
Solution:
You should use Javascript, this is a solution using JQuery:
$( "select" ).change(function() {
var id = $(this).next().next().val();
if($(id)==1){
$("tr:nth-of-type(3)").append("<td>Size</td>
<td><textarea name='description' class='form-control'></textarea></td>");}
else
{
$("tr:nth-of-type(3)").append("<td>Weight</td>
<td><textarea name='description' class='form-control'></textarea></td>");}
});
You can add more classes to easily select the wanted element, then change the script upon.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: can't write image data to path
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.