问题描述
我有这个项目,我必须为相应的选定选项(即“小”或“大”)声明“宽度”和“高度”的子变量。例如,如果用户从选项中选择“小”,那么系统应该为“小”保存两个变量“宽度”和“高度”。 非常感谢您对这个问题的帮助。 如果您有进一步的问题,请提出。谢谢
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="js.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<!--name.PHP to be called on form submission-->
<form method = 'post'>
<h4>CALculaTOR</h4>
<label>Select Plate Size</label>
<select name = 'plateSelector'>
<option value = 'Small Plate' name="small">Small Plate</option>
<option value = 'Large Plate' name="large">Large Plate</option>
</select>
<input type = 'submit' name = 'submit' value = Submit>
</form>
</body>
<script type="text/javascript">
</script>
</html>
<?PHP
// Check if form is submitted successfully
if(isset($_POST["submit"]))
{
// Check if any option is selected
if(isset($_POST["plateSelector"]))
{
$plate = $_GET["small"];
if($plate)
{
$height=5;
$width=5;
print "$height and $width<br/>";
}
else{
print"this";
}
}
else
echo "Select an option first !!";
}
?>
@H_404_4@
解决方法
有很多方法可以完成这项工作(例如,您可以将两条数据存储在一个 db 中),但其中一种更简单的方法是将两个变量(宽度和高度)放入一个字符串中,然后将它们分开它们由分隔符(例如##)。
例如,字符串 12.2##22.2 可以代表 width=12.2 和 height=22.2
您可以参考以下工作代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="js.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<!--name.php to be called on form submission-->
<form method ='post' action="">
<h4>CALCULATOR</h4>
<label>Select Plate Size</label>
<select name = 'plateSelector'>
<option value = '' >Please choose</option>
<option value = '12.2##22.2' >Small Plate</option>
<option value = '100.2##99.1' >Large Plate</option>
</select>
<input type = 'submit' name = 'submit' value = Submit>
</form>
</body>
<script type="text/javascript">
</script>
</html>
<?php
if(isset($_POST["submit"])) {
$plate = $_POST["plateSelector"];
if($plate) {
$pieces = explode("##",$plate);
echo "width is: ". $pieces[0] . ",height is: " . $pieces[1];
} else {
echo "Please select an option";
}
}
?>