简体   繁体   English

变灰HTML表单元素

[英]Graying out HTML form elements

I want to let the user select one "row" to use to submit the with to request a report type. 我想让用户选择一个“行”以用于提交请求一个报告类型。 How can I put radio buttons in the first column of a table and whichever is selected is the active row that gets sent to the next page via the submit button? 如何将单选按钮放在表格的第一列中,而选中的哪个是通过“提交”按钮发送到下一页的活动行?

I think Andreas is on the right track, but it's not as useful as it could be. 我认为安德里亚斯(Andreas)处在正确的轨道上,但它没有发挥应有的作用。 This should be a bit better: 这应该更好一些:

<?php 
blah ...

echo <<<HTML
    <form action="handler.php" action="post">
        <table>
HTML;

foreach ($rows as $row)
{
    $id = $row['id'];
    $text = $row['text'];  // escape this unless you know it's safe

    echo <<<HTML
        <tr>
            <td><input type="radio" value="$id" name="theRadioButton" /></td>
            <td><input type="text" name="textfield_$id" value="$text" /></td>
        </tr>
HTML;
}

echo <<<HTML
    </table>
</form>
HTML;

form handler: 表单处理程序:

<?php 
    $id = isset($_POST['theRadioButton']) ? $_POST['theRadioButton'] : null;

    if ($id)
    {
        $textfield = $_POST["textfield_$id"];
    }
?>

If you want to do it in pure PHP, i guess you could do this: 如果您想用纯PHP做到这一点,我想您可以这样做:

<form action="ascript.php" action="post">
    <table>
        <tr>
            <td><input type="radio" value="row1" name="theRadioButton" /></td>
            <td><input type="text" name="row1textfield" /></td>
        </tr>
        <tr>
            <td><input type="radio" value="row2" name="theRadioButton" /></td>
            <td><input type="text" name="row2textfield" /></td>
        </tr>
        <tr>
            <td><input type="radio" value="row3" name="theRadioButton" /></td>
            <td><input type="text" name="row3textfield" /></td>
        </tr>
    </table>
</form>

ascript.php ascript.php

<?php 
    if ($_POST['theRadioButton'] == "row1") {
        echo $_POST['row1textfield'];
        // Handle row 1 ..
    }
    else if ($_POST['theRadioButton'] == "row2") {
        echo $_POST['row2textfield'];
        // Handle row 2 ..
    }
    else if ($_POST['theRadioButton'] == "row3") { 
        echo $_POST['row3textfield'];
        // Handle row 3 ..
    }
?>

However, if you're willing to use some jQuery, you could just name the textfields the same thing and disable the fields you're not going to use. 但是,如果您愿意使用某些jQuery,则可以将文本字段命名为相同的名称,并禁用将不使用的字段。 Here's a fiddle: http://jsfiddle.net/rrvQu/1/ 这是一个小提琴: http : //jsfiddle.net/rrvQu/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM