Php 关注GET请求后的HTML选择下拉选项
因此,我正在制作一个程序,并向自己提交一份表格。这个程序是一个非常基本的计算器,因为我不熟悉HTML和PHP。我正在尝试这样做,当您提交表单时,选择下拉列表将保留在最近使用的运算符上 例如,如果我让计算器执行“5+5”,那么我希望提交的表单将运算符下拉列表保留在“+”上 这是我的密码:Php 关注GET请求后的HTML选择下拉选项,php,html,Php,Html,因此,我正在制作一个程序,并向自己提交一份表格。这个程序是一个非常基本的计算器,因为我不熟悉HTML和PHP。我正在尝试这样做,当您提交表单时,选择下拉列表将保留在最近使用的运算符上 例如,如果我让计算器执行“5+5”,那么我希望提交的表单将运算符下拉列表保留在“+”上 这是我的密码: <?php // grab the form values from $_HTTP_GET_VARS hash extract($_GET); // first compute the output,
<?php
// grab the form values from $_HTTP_GET_VARS hash extract($_GET);
// first compute the output, but only if data has been input if(isset($calc) && $operator == "multiply") { // $calc exists as a variable
$prod = $x * $y; } elseif (isset($calc) && $operator == "plus") {
$operator = $plus;
$prod = $x + $y; } elseif (isset($calc) && $operator == "minus") {
$operator = "minus";
$prod = $x - $y; } elseif (isset($calc) && $operator == "divide") {
$sign = "/";
$prod = $x / $y; } else { // set defaults
$x=0;
$y=0;
$prod=0; } ?>
<html> <head>
<title>PHP Calculator Example</title> </head>
<body>
<h3>PHP Calculator (Version 1)</h3>
<p>Multiply two numbers and output the result</p>
<form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">
<label for="x">x = </label>
<input type="text" name="x" id="x" size="5" value="<?php print $x; ?>"/>
<select name="operator" id="operator">
<option value="plus">+</option>
<option value="minus">-</option>
<option value="divide">/</option>
<option value="multiply">*</option>
</select>
<label for="y">y = </label>
<input type="text" name="y" id="y" size="5" value="<?php print $y; ?>"/>
<input type="submit" name="calc" value="Calculate"/>
</form>
<!-- print the result -->
<?php if(isset($calc)) {
print "<p>x $sign y = $prod</p>";
} ?>
</body> </html>
PHP计算器示例
PHP计算器(第1版)
将两个数字相乘并输出结果
为了保持在“选择”元素中选定的选项上
该选项必须有一个属性“selected”
value=“减”>-
value=“multiply”>*
希望它能帮助您。使用javascript的一个可能解决方案是
<?php
if(isset($_GET['operator'])) {
?>
<script>document.getElementById("operator").value = '<?=$_GET['operator']?>';</script>
<?php
}
?>
document.getElementById(“运算符”)。值=“”;
将选定的属性添加到下拉列表的开头。例如
<?php
if(isset($_GET['operator'])) {
?>
<script>document.getElementById("operator").value = '<?=$_GET['operator']?>';</script>
<?php
}
?>