单选按钮,更改背景颜色 PHP

Posted

技术标签:

【中文标题】单选按钮,更改背景颜色 PHP【英文标题】:Radio buttons, changing the background color PHP 【发布时间】:2015-01-03 16:48:04 【问题描述】:

好的,伙计们,基本上我有一个表格。当用户点击提交时,这个表单应该输出用户的选择,或者文本,还有用户选择的背景颜色。

目前正在工作的是文本...和复选框。我无法让下拉选择输出......当涉及到背景颜色时......我不知道从哪里开始......有点冗长......对此感到抱歉,只需要一些方向。它应该在输出中显示的示例如下:http://omega.uta.edu/~cyjang/ctec4309/labex/php2/post_form_3a.php 填完就知道了..

这是我的 html

<hr size="1">

<h3>Form</h3>
* required fields
 <form action= "post3.php" method="post">
    Author * : <input type="text" name="author"><br/>
    Email : <input type="text" name="email"><br/>
    Title * : <input type="text" name="title"><br/>

Tag: 
<input type="checkbox" name="tag[]" value="General Interests"> General Interests
<input type="checkbox" name="tag[]" value="Local Schools"> Local Schools
<input type="checkbox" name="tag[]" value="Safety"> Safety
<br/>

 City *:
<select name="mycity">
<option value="Arlington" name"city">Arlington</option>
<option value="Dallas" name"city">Dallas</option>
<option value="FTW" name"city">Fort Worth</option>
</select> 
<br/>

 Background color *:
<input type="radio" name="bgcolor" value"yellow">  Yellow 
<input type="radio" name="bgcolor" value"blue">  Blue  
<br/>




Comment * : <br/><textarea name="comment" rows="5" cols="40"></textarea><br>
<input type="Submit" name="SubmitThis" value="Preview">


  </form> 

这里是我的 PHP:

//==========================
// Data validation
//==========================

// check to see if there is a form submission or not
if (array_key_exists("SubmitThis", $_POST)) 

// data validation
//  - check required fields

//== Modify the required and expected arrays below to fit your form ========
$required = array('title', 'author','comment','bgcolor', 'city');
$expected = array('title', 'author','comment','tag', 'email', 'city');
$missing = array();

// use foreach loop to run through each item in the expected array
foreach($expected as $thisField) 
    // setup a variable to store user input for this field name
    $thisUserInput = $_POST[$thisField];

    // check if this field is a required field
    if (in_array($thisField, $required)) 
        // check if user input of this field is empty, if yes, add this field to the missing array
        if (empty($thisUserInput)) 
            array_push($missing, $thisField);
         else 

            $$thisField = $thisUserInput;
        
     else 
        $$thisField = $thisUserInput;
    



    // after running through all expected fields, check the $missing array. if there is no required field missing, the $missing array will be empty.
    if (empty($missing))
    // empty($missing) is true --> no missing field, proceed with business processes (in this example, display all user input.)

    // deal with array input, ex. $tag
    $tagStr = implode(", ", $tag);

    // print_r ($tag); // enable this line will print the $tag array, so you can see what's been stored in the $tag array.  It may help you to debug.

    // process author name and email
    if (!empty($email)) 
        $author = "<a href='mailto:$email'>$author</a>";
    

    $output = "<p> <table border=2 cellpadding=5>
            <tr><th> Author:</th><td> $author </td></tr>
            <tr><th> Title:</th><td> $title </td></tr>
            <tr><th> Tag:</th><td> $tagStr </td></tr>
            <tr><th> City:</th><td> $city </td></tr>
            <tr><th> Comment:</th><td> <br>$comment </td></tr>
            </table></p>";

     else 
    // empty($missing) is false --> $missing array is not empty -- prepare a message for the user

    $missingFieldList = implode(", ",$missing);
    $output = "The following fields are missing from your post, please go back and fill them in.  Thank you. <br>
                    <b>Missing fields: $missingFieldList </b>
                ";




 else 
$output = "Please post your message use <a href='post_form_3.php'>this form</a>.";




?>

【问题讨论】:

【参考方案1】:

在下拉列表中,&lt;option&gt; 只能有一个value="" 属性,而不是name=""。名称只能在&lt;select&gt; 部分:

<select name="city">
    <option value="Arlington">Arlington</option>
    <option value="Dallas">Dallas</option>
    <option value="FTW">Fort Worth</option>
</select> 

对于颜色,只需使用简单的样式表 (css):

$output = "
    <style>
        th,td  background-color: $bgcolor; 
    </style>";

或者,如果您创建一个名为“黄色”和“蓝色”的 css 类,您可以分配该类:

 $output .= "
    <table border=2 cellpadding=5 class=\"$bgcolor\">
        <tr><th> Author:</th><td> $author </td></tr>
        <tr><th> Title:</th><td> $title </td></tr>
        <tr><th> Tag:</th><td> $tagStr </td></tr>
        <tr><th> City:</th><td> $city </td></tr>
        <tr><th> Comment:</th><td> <br>$comment </td></tr>
    </table>";

CSS 文件:

table.yellow td,
table.yellow th  background-color: #FFFF00; 

table.blue td,
table.blue th  background-color: #0000FF; 

【讨论】:

谢谢。我有显示输出的下拉菜单。虽然仍然没有得到背景颜色。我可以在
区域中使用 而不是使用外部 css,对吗?嗯嗯仍然没有得到任何颜色。等等,我需要定义 $bgcolor 吗?
当用户在表单上选择黄色时,如何让它输出黄色背景色?如果客户在表单上选择蓝色,我该如何输出蓝色背景? 是的,您确实需要从 $_POST 定义 $bgcolor 然后它应该可以工作 welp 大声笑我没有得到任何地方。让我们看看......到目前为止,我有你在上面发布的内容,并添加到 $bgcolor=$_POST['bgcolor'];不知道之后该怎么办?我知道我需要对 HTML 表单代码做一些事情......只是不确定我放了什么。 如果可以的话,给我一个小时左右,我不在电脑前提供更多帮助

以上是关于单选按钮,更改背景颜色 PHP的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 3 中使用 tkinter 更改单选按钮的背景颜色

js改变单选按钮背景图片,怎么更改文字颜色呢?效果如图

材质 UI 切换按钮 - 选择时无法更改背景颜色

更改 Qt 中的单选按钮文本颜色

如何更改单选按钮悬停颜色

C ++ MFC更改单选框的背景颜色