从 php 中的所选选项声明两个变量
Declare two variables from the selected option in php
我有这个项目,我必须在其中为相应的 selected 选项声明“宽度”和“高度”的子变量,即“小”或“大”。例如,如果用户 select 从选项中选择“small”,那么系统应该为“small”保存两个变量“width”和“height”。
非常感谢您对这个问题的帮助。
如果您还有其他问题,请尽管提问。谢谢
<!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 !!";
}
?>
有很多方法可以完成这项工作(例如,您可以将两条数据存储在一个数据库中),但是一种更简单的方法是将两个变量(宽度和高度)放入一个字符串中,然后然后用分隔符分隔它们(例如##)。
例如字符串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";
}
}
?>
我有这个项目,我必须在其中为相应的 selected 选项声明“宽度”和“高度”的子变量,即“小”或“大”。例如,如果用户 select 从选项中选择“small”,那么系统应该为“small”保存两个变量“width”和“height”。 非常感谢您对这个问题的帮助。 如果您还有其他问题,请尽管提问。谢谢
<!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 !!";
}
?>
有很多方法可以完成这项工作(例如,您可以将两条数据存储在一个数据库中),但是一种更简单的方法是将两个变量(宽度和高度)放入一个字符串中,然后然后用分隔符分隔它们(例如##)。
例如字符串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";
}
}
?>