用PHP呈现条件HTML元素



我正在尝试学习PHP来解决Assesment的挑战,在那里我必须构建一个产品列表&添加产品页面。阅读这里和那里,观看一些教程,我能够开发到目前为止的代码:

Index.hp:

<?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大小、BOOK Weight和Forniture尺寸(H、W和L(的输入应该根据#prod_type上的用户选择进行渲染。目前,这些输入显示在用户选择一个选择选项后,并点击添加产品按钮(与我按下的POST方法有关,是我得到的最接近的方法(

PHP完全在服务器上运行。如果你希望用户与你的网页交互时(不需要往返于服务器(浏览器中发生一些事情,Javascript就是你的朋友。

在这种情况下,我会给每个输入一个data属性,说明它们与哪种产品类型相关。然后,您可以编写一个Javascript函数来处理对prod_type字段的更改,并显示/隐藏正确的字段。

例如:

function toggleFields() {
var productType = document.getElementById('prod_type').value;
var fields = document.querySelectorAll('[data-if-prod-type]');

fields.forEach(function (field) {
if (field.dataset.ifProdType === productType) {
field.style.display = '';
} else {
field.style.display = 'none';
}
});
}
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('prod_type').addEventListener('change', toggleFields);

// Run the toggle function when the DOM is ready loading,
// so that fields that are not relevant to #prod_type's
// initial value are hidden.
toggleFields();
});
<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>
<input type="text" name="dvd_size" class="form-control" placeholder="Enter DVD Size" data-if-prod-type="DVD">
<input type="text" name="book_weight" class="form-control" placeholder="Enter Book Weight" data-if-prod-type="BOOK">
<div data-if-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">
</div>

您可以尝试AJAX。它很容易使用,而且你会喜欢它的功能。使用AJAX可以很容易地完成您在页面中想要做的事情。如果你对Javascript和jQuery有一点了解,你一定会喜欢它的。W3School可以向您详细解释它-AJAX简介

AJAX在代码中的使用示例,

index.php:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Index Page</title>
</head>
<body>
<?php
if(isset($_POST['prod_type'])){
print_r($_POST); die;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="prod_type" onchange="getProdOptions(this.value)">
<option value="">Select Product Type</option>
<option value="DVD">DVD</option>
<option value="BOOK">BOOK</option>
<option value="FUR">FURNITURE</option>
</select>
<div id="option_area"></div>
<input type="submit" name="add">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> // CDN link of jQuery, you can get from - https://cdnjs.com/libraries/jquery
<script type="text/javascript">
function getProdOptions(val) {
val = val.trim();
if(val !== '') {
$.ajax({
url: "/ajax_request.php", // url is the destination to receive the request
data: {
prod_type: val
}, // data is the key and the value you are sending
type: "POST", // method is GET, POST, etc..
success: function( response ) {
response = JSON.parse( response );
if( response.status ){
document.getElementById( 'option_area' ).innerHTML = response.data; // Pure JS Syntax
}else{
alert('No data for the selected prod type.');
}
}
});
}else{
$("#option_area").html(""); // jQuery Syntax
}
}
</script>

ajax_request.php:

<?php
if( isset( $_POST['prod_type'] ) && !empty( $_POST['prod_type'] ) ) {
$response = ['status' => true];
// any condition to check and the condition not met thenn send the status as false
$html = '<input type="text" name="dvd_size" class="form-control" placeholder="Enter DVD Size">';
$html .= '<input type="text" name="book_weight" class="form-control" placeholder="Enter Book Weight">'; // `.=` will concatenate the strings
$response['data'] = $html;
echo json_encode( $response );
}
?>