在 PHP 中调用并声明选择框!

发布于 2024-10-21 05:25:14 字数 1005 浏览 1 评论 0原文

我希望能够根据用户选择的选项来更改网站背景的颜色。

我的选择框有以下代码:

<select name="change_date" >
<option value="1" id="1">1</option>
<option value="2" id="2">2</option>
<option value="3" id="3">3</option>
</select>

使用 PHP,我如何获得它,以便它简单地更改为红色(1)、绿色(2)和粉红色(3)?

这是我尝试过的代码(不成功且完全猜测):

<?php
if(isset($_POST['change_date'])=='1' )
   {
   echo "<body style='background-color:red;'></body>";
   }else{
   echo "failed";
   }
if(isset($_POST['change_date'])=='2' )
   {
   echo "<body style='background-color:green;'></body>";
   }else{
   echo "failed";
   }
if(isset($_POST['change_date'])=='3' )
   {
   echo "<body style='background-color:pink;'></body>";
   }else{
   echo "failed";
   }

?>

有什么建议吗?方法?链接?

更新: 我已经尝试了所有方法,但似乎没有一个有效。这一定是我做错了什么。 我想要的是当用户选择选项(即 1,2 或 3)并单击“发送”时,颜色就会改变。 希望这有更多帮助。我忘记添加之前我想要一个发送按钮必须被点击然后所有聪明的事情都会发生。 谢谢

I want to be able to change the color of my websites background depending on which option the user chooses.

I have this code for my select box:

<select name="change_date" >
<option value="1" id="1">1</option>
<option value="2" id="2">2</option>
<option value="3" id="3">3</option>
</select>

Using PHP, how would i get it so that it simply changed to red for 1, green for 2 and pink for 3?

This is the code I have tried (unsuccessfully and complete guesswork):

<?php
if(isset($_POST['change_date'])=='1' )
   {
   echo "<body style='background-color:red;'></body>";
   }else{
   echo "failed";
   }
if(isset($_POST['change_date'])=='2' )
   {
   echo "<body style='background-color:green;'></body>";
   }else{
   echo "failed";
   }
if(isset($_POST['change_date'])=='3' )
   {
   echo "<body style='background-color:pink;'></body>";
   }else{
   echo "failed";
   }

?>

Any suggestions? methods? links?

UPDATE:
I have tried all methods and none seem to work guys. It must be something I am doing wrong.
What i want is when the user chooses and option ie 1,2 or 3 and clicks send, then the color will change.
Hope this helps more. I forgot to add before that I want a send button to have to be clicked then all the clever stuff happens.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

长不大的小祸害 2024-10-28 05:25:14
if (isset($_POST['change_date']))
{
    switch ($_POST['change_date'])
    {
        case 1: $color = 'red'; break;
        case 2: $color = 'green'; break;
        case 3: $color = 'pink'; break;
        default: die('failed');
    }
    echo "<body style='background-color:$color;'></body>";
}
if (isset($_POST['change_date']))
{
    switch ($_POST['change_date'])
    {
        case 1: $color = 'red'; break;
        case 2: $color = 'green'; break;
        case 3: $color = 'pink'; break;
        default: die('failed');
    }
    echo "<body style='background-color:$color;'></body>";
}
栖竹 2024-10-28 05:25:14

使用开关:

$color = !empty($_POST['change_date'])?$_POST['change_date']:0;

switch ($color) {
    default:
    case 1:
           echo "pink";
    break;
    case 2: 
           echo "orange";
    break;
}

应该做你想做的事。有很多其他方法可以使用数组等来实现这一点。这就是我选择向您展示的方式:)

编辑:
数组方法

$colors = array(1 => 'pink', 2 => 'orange');
$color = !empty($_POST['change_date'])?$_POST['change_date']:1;

echo "<body style='background-color:" . $colors[$color] . ";'></body>";

两者都应该可以工作,等待我犯的任何错误。

Use a switch:

$color = !empty($_POST['change_date'])?$_POST['change_date']:0;

switch ($color) {
    default:
    case 1:
           echo "pink";
    break;
    case 2: 
           echo "orange";
    break;
}

Should do what you want. Plenty of other ways to do it with arrays etc. Just the way I chose to show you :)

EDIT:
Array Method

$colors = array(1 => 'pink', 2 => 'orange');
$color = !empty($_POST['change_date'])?$_POST['change_date']:1;

echo "<body style='background-color:" . $colors[$color] . ";'></body>";

Both should work, pending any errors I made.

美男兮 2024-10-28 05:25:14

仅当变量“change_date”来自通过 POST 方法的查询字符串时,您的 PHP 代码才有效...

您需要动态设置颜色吗?或者发送表格后?

your PHP code only works if the variable "change_date" comes from a query string via a POST method...

Do you need to set the color on the fly? or after sending a form?

枫林﹌晚霞¤ 2024-10-28 05:25:14

您的问题出在您对 isset 的使用上。该函数简单地返回一个布尔值,而不是字段的值。尝试以下方法:

if(isset($_POST['change_date']))
{
    switch($_POST['change_date'])
    {
        case 1:
            echo "<body style='background-color:red;'></body>";
            break;
        case 2:
            echo "<body style='background-color:green;'></body>";
            break;
        case 3:
            echo "<body style='background-color:pink;'></body>";
            break;
        default:
            echo "<body style='background-color:white;'></body>";
    }
}
else
{
    echo "<body style='background-color:white;'></body>";
}

You're problem is in your use of isset. This function simple returns a boolean value, not the value of the field. Try the below:

if(isset($_POST['change_date']))
{
    switch($_POST['change_date'])
    {
        case 1:
            echo "<body style='background-color:red;'></body>";
            break;
        case 2:
            echo "<body style='background-color:green;'></body>";
            break;
        case 3:
            echo "<body style='background-color:pink;'></body>";
            break;
        default:
            echo "<body style='background-color:white;'></body>";
    }
}
else
{
    echo "<body style='background-color:white;'></body>";
}
浪漫人生路 2024-10-28 05:25:14

另一种方法可能是,如果你不想使用 switch 语句,

$color = isset($_POST['change_date']))?$_POST['change_date']:0;
if($color){

               if($color == 1) echo "<body style='background-color:red;'></body>";
               if($color == 2) echo "<body style='background-color:green;'></body>";
               if($color == 3) echo "<body style='background-color:pink;'></body>";
            }

Another way could be, if you dont wanna use switch statement ,

$color = isset($_POST['change_date']))?$_POST['change_date']:0;
if($color){

               if($color == 1) echo "<body style='background-color:red;'></body>";
               if($color == 2) echo "<body style='background-color:green;'></body>";
               if($color == 3) echo "<body style='background-color:pink;'></body>";
            }
流殇 2024-10-28 05:25:14

尝试使用值映射数组,正如其他答案之一所指出的,它可能是 GET 而不是 POST,所以我使用 $_REQUEST 作为示例:

<?php
   $colors = array(
      1 => "red",
      2 => "green",
      3 => "pink",
   );

   if ($c = $colors[ $_REQUEST["change_date"] ])
   {
       echo "<body style='background-color: $c;'>body</body>";
   }
   else {
       echo "failed";
   }

Try a value map array, and as pointed out in one of the other answers it might be GET instead of POST, so I'm using $_REQUEST as example:

<?php
   $colors = array(
      1 => "red",
      2 => "green",
      3 => "pink",
   );

   if ($c = $colors[ $_REQUEST["change_date"] ])
   {
       echo "<body style='background-color: $c;'>body</body>";
   }
   else {
       echo "failed";
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文