gpt4 book ai didi

PHP:以 html 形式与 'selected values' 交互

转载 作者:行者123 更新时间:2023-11-28 00:06:10 25 4
gpt4 key购买 nike

我成功运行了一个与 html 页面交互的 php 页面。但是,有一个实例接近尾声时 php 没有按预期进行交互。

我遇到的问题是,无论选择什么颜色,或者根本没有选择任何颜色,页面总是执行“Andrew hates you”。如果用户选择了“RED”,我只希望显示此消息。如果选择了“GREEN”、“YELLOW”、“BLUE”或“PURPLE”,我希望显示消息“Andrew likes you”。

HTML 页面:

 <html>
<head>
<title>Test</title><br />
</head>
<body>
<form action="website.php" method="POST">
<p><strong>Name:</strong></br>
<input type="text" name="user"/></p>
<p><strong>Age:</strong><br/>
<input type="text" name="age"/></p>
<p><strong>What's your favourite colour?</strong><br/><br>
<select name="colour[]" multiple="multiple">
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Yellow">Yellow</option>
<option value="Purple">Purple</option>
</select>
<p><input type="submit" value="Submit"/></p>
</form>


</body>
</html>

PHP 页面:

 //the first part of this php page works perfectly

<?php
echo "<p>Hi <b>". $_POST["user"]."!</p>";
echo "<p>You are <b>".$_POST["age"]."</b><p>";
echo "<p> Your favourite colour is: <br/>";
if (!empty($_POST["colour"])) {
echo "<ul>";
foreach ($_POST ["colour"] as $value) {
echo "<li>$value</li>";
}
echo "</ul>";
}
?>

//this is where things start to break down

<?php

$funny_colour = "Red";

if ($_POST["$value"] = $funny_colour) {
$message = "Andrew hates you"; // Always shows no matter what
echo $message;
} else if ($_POST["$value"] != $funny_colour) {
$message = "Andrew likes you"; // Never shows
echo $message;
}

?>

感谢您的帮助,我真的很感激,并会尽力回答您的一些问题。

最佳答案

您将获得 $_POST["colour"] 作为数组。


$funny_colour = "Red";

foreach ($_POST ["colour"] as $value) {
if ($value == $funny_colour) {
$message = "Andrew hates you"; // Always shows no matter what
echo $message;
} else if ($value != $funny_colour) {
$message = "Andrew likes you"; // Never shows
echo $message;
}
}

关于PHP:以 html 形式与 'selected values' 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18397635/

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