【发布时间】:2022-01-11 02:31:03
【问题描述】:
我正在尝试学习 PHP 来解决评估挑战,我必须在其中构建产品列表和添加产品页面。 在这里和那里阅读,观看一些教程,到目前为止我能够开发此代码:
索引.php:
<?php include("db.php"); ?>
<?php include("includes/header.php"); ?>
<div class="container p-4">
<div class="row">
<div class="col-md-4">
<div class="card card-body">
<!--INPUT FORM
it will contains the form to add new product to the database:
Fields: SKU / NAME / PRICE / PROD_TYPE / DVD / BOOK / FUR_H / FUR_W / FUR_L -->
<form action="index.php" method="POST">
<div class="form-group">
<input type="text" name="sku" class="form-control" placeholder="Enter SKU Code">
<hr/>
<input type="text" name="name" class="form-control" placeholder="Enter Product Name">
<hr/>
<input type="text" name="price" class="form-control" placeholder="Enter Price">
<hr/>
<label>Product Type</label>
<select id="prod_type" name="prod_type" class="form-control" >
<option value="">Select Product Type</option>
<option value="DVD">DVD</option>
<option value="BOOK">BOOK</option>
<option value="FUR">FURNITURE</option>
</select>
<!-- <hr/> -->
<!--if the select(prod_type) option = DVD, then show the following fields:
Fields: DVD_SIZE
if the select(prod_type) option = BOOK, then show the following fields:
Fields: BOOK_WEIGHT
if the select(prod_type) option = FUR, then show the following fields:
Fields: FUR_H / FUR_W / FUR_L -->
<hr/>
<?php if(isset($_POST['prod_type']) && $_POST['prod_type'] == 'DVD'){ ?>
<input type="text" name="dvd_size" class="form-control" placeholder="Enter DVD Size">
<?php } else if(isset($_POST['prod_type']) && $_POST['prod_type'] == 'BOOK'){ ?>
<input type="text" name="book_weight" class="form-control" placeholder="Enter Book Weight">
<?php } else if(isset($_POST['prod_type']) && $_POST['prod_type'] == 'FUR'){ ?>
<hr/>
<input type="text" name="fur_h" class="form-control" placeholder="Enter Furniture Height">
<hr/>
<input type="text" name="fur_w" class="form-control" placeholder="Enter Furniture Width">
<hr/>
<input type="text" name="fur_l" class="form-control" placeholder="Enter Furniture Length">
<?php } ?>
<!-- <hr/> -->
</div>
<hr/>
<input type="submit" name="add_product" class="btn btn-success w-100" value="Add Product">
</form>
</div>
</div>
<div class="col-md-8">
</div>
</div>
</div>
<?php include("includes/footer.php"); ?>
问题是,DVD 尺寸、书本重量和 Forniture 尺寸(H、W 和 L)的输入应根据用户在 #prod_type 上的选择进行渲染。目前,这些输入在用户选择一个选择选项后显示,然后点击添加产品按钮(与我按下的 POST 方法相关,是我得到的最接近的)
【问题讨论】:
-
或者您可以快速浏览一下 AJAX 技术,该技术可以让您修改页面而无需往返服务器并返回整个页面
标签: php forms html-select conditional-rendering