Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用表单和php更改html页面的背景色?_Php_Html_Forms - Fatal编程技术网

如何使用表单和php更改html页面的背景色?

如何使用表单和php更改html页面的背景色?,php,html,forms,Php,Html,Forms,输入所选颜色后,页面应更改其背景色。应该使用html表单来完成 <body bgcolor="<?php $_GET["color"]; ?>"> <form method="get" name="color"> Background color:<br> <input type="text" name="color"> <input type="submit" value=

输入所选颜色后,页面应更改其背景色。应该使用html表单来完成

<body bgcolor="<?php $_GET["color"]; ?>">
    <form method="get" name="color">
        Background color:<br>
        <input type="text" name="color">
        <input type="submit" value="submit">
    </form>
</body>

您必须回显变量
$\u GET[“color”]
中存储的值,如

<body bgcolor="<?php echo $_GET["color"]; ?>">

您应该使用CSS而不是HTML属性来装饰HTML呈现。此外,您还应该知道哪个组织代表更好的组织,因为它是分离语言的(=将CSS与HTML分离)

因此,要回答您的问题,您可以这样做:

<?php
    if (!isset($_GET["color"]) || $_GET["color"] === '')
    {
        $_GET["color"] = 'pink';
    }
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>My Website</title>
        <style>
            body {
                background-color: '<?= $_GET["color"] ?>';
            }
        </style>
    </head>
    <body>
        <form method="get" name="color">
            Background color:<br>
            <input type="text" name="color">
            <input type="submit" value="submit">
        </form>
    </body>
</html>

我的网站
身体{
背景色:'';
}
背景色:
    <?php if(!empty($_GET['color']) {?>
        <body style="background-color:'<?php echo $_GET['color'];?>">
    <?php }else {?>
        <body style="background-color:orange">
    <?php } ?>
    <form method="get" name="formcolor">
            Background color:<br>
            <input type="text" name="color">
            <input type="submit" value="submit">
        </form>
    </body>
<?php
    if (!isset($_GET["color"]) || $_GET["color"] === '')
    {
        $_GET["color"] = 'pink';
    }
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>My Website</title>
        <style>
            body {
                background-color: '<?= $_GET["color"] ?>';
            }
        </style>
    </head>
    <body>
        <form method="get" name="color">
            Background color:<br>
            <input type="text" name="color">
            <input type="submit" value="submit">
        </form>
    </body>
</html>