我正在尝试创建一个BMI计算器。这应该允许人们使用公制或英制测量。
我意识到我可以使用隐藏的标签来解决我的问题,但这之前我已经错过了所以我想我会问:我可以使用$_POST['variableName']
来查找提交的variableName字段值;但是......我不知道,或者看看如何验证使用哪个表单来提交变量。
我的代码如下(虽然我不确定它与问题严格相关):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li><a href="#metric">Metric</a></li>
<li><a href="#imperial">Imperial</a></li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
我确认它有效(虽然目前没有验证 - 我不想过多地提出我的问题)和公制;我已经添加了表格,但没有为帝国加工。
答案 0 :(得分:86)
要识别提交的表单,您可以使用:
表单的名称不会作为POST数据的一部分发送到服务器。
您可以使用以下代码
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
答案 1 :(得分:14)
你可以这样做:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
检查发布的值
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
答案 2 :(得分:13)
未提交表单名称。您应该只为每个表单添加一个隐藏字段,并将其称为一天。
答案 3 :(得分:8)
在表单提交按钮中(表单的id方法为post
):
<input type="submit" value="save" name="commentData">
在PHP文件中:
if (isset($_POST['commentData'])){
// Code
}
答案 4 :(得分:5)
出于某种原因,当使用Ajax / jQuery提交时,提交按钮的名称不会传递给超级全局$_POST
。
以防它有助于某人...
答案 5 :(得分:2)
正如petervandijck.com指出的那样,如果您将此代码置于某种登录系统之后或将其嵌入其他代码中,则此代码可能容易受到XSS攻击。
要防止XSS攻击,请写下:
<?php echo "$weight"; ?>
你应该写:
<?php echo htmlentities($weight); ?>
甚至可以更好地写成:
<?=htmlentities($weight); ?>
答案 6 :(得分:2)
您可以在表单的action参数中使用GET,每当我创建登录/注册组合页面时,我都会使用GET。
例如:action="loginregister.php?whichform=loginform"
希望这有帮助!
答案 7 :(得分:1)
在每个表单的提交按钮上使用唯一值,如此
<强>的index.html 强>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
<强> email.php 强>
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
答案 8 :(得分:1)
我遇到了类似的问题,这使我想到了这个问题。我回顾了前面所有的答案,但最终我最终找到了自己的解决方案。我希望它可以帮助其他人解决同样的问题:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
它无缝有效地完成了以下任务:
答案 9 :(得分:0)
仅提交表单字段的名称,表单名称本身不是。但是你可以设置一个带有名字的隐藏字段。
答案 10 :(得分:0)
你用echo $ height确实意识到了这一点;你在你的应用程序中打开了一个非常非常严重的安全漏洞,对吗?
答案 11 :(得分:0)
您只需为提交按钮指定一个名称,然后根据该操作执行需要完成的操作。我在页面上有几个表单,并做到这一点。传递按钮名称,然后如果按钮名称=按钮名称执行某些操作。