gpt4 book ai didi

PHP isset 函数返回表单中的所有选项

转载 作者:行者123 更新时间:2023-12-03 23:04:52 27 4
gpt4 key购买 nike

有人可以帮助我学习 PHP 吗?我想创建简单的表单,并在添加“isset 函数”后返回所有表单。

<html>
<body>
<div id="main">
<form action = "" method = 'POST'>
<select name = "something">
<option value = "Monitors">Monitors</option>
<option value = "Graphics">Graphics</option>
<option value = "Peripherials">Peripherials</option>
<option value = "Processors">Processors</option>
</select>
<input type = "submit" value = "OK">
</form>
</div>


<?php


if (isset($_POST["something"]) == "Monitors")
{
echo "Monitors <br />";
}

if (isset($_POST["something"]) == "Graphics")
{
echo "Graphics <br />";
}

if (isset($_POST["something"]) == "Peripherials")
{
echo "Peripherials <br />";
}

if (isset($_POST["something"]) == "Processors")
{
echo "Processors <br />";
}

?>


</body>
</html>

选择表单中的选项之一后,它会返回所有“回显”。不使用“isset”是可以的,但随后会返回“ undefined variable ”的错误。我哪里出错了?

最佳答案

原因:- 基于 PHP: isset - Manual

Returns TRUE if var exists and has value other than NULL. FALSE otherwise.

因为你的 if 条件变成:-

if(true == 'Monitors'){

始终被视为真实,并且一切都在打印。

解决方案:-

更改 if 条件,如下所示:-

if(isset($_POST["something"])  &&  $_POST["something"]== "Monitors"){

其他人也是如此。

此外,您的 Php 代码不必要太长,请使其简短,如下所示:-

<?php
if (isset($_POST["something"])){
echo $_POST["something"]; // add ."<br />" if you needed
}
?>

完整的代码需要是:-

<html>
<body>
<div id="main">
<form action = "" method = 'POST'>
<select name = "something">
<option value = "Monitors">Monitors</option>
<option value = "Graphics">Graphics</option>
<option value = "Peripherials">Peripherials</option>
<option value = "Processors">Processors</option>
</select>
<input type = "submit" value = "OK">
</form>
</div>

<?php
if (isset($_POST["something"])){
echo $_POST["something"]; // add ."<br />" if you needed
}
?>

关于PHP isset 函数返回表单中的所有选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299416/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com