简体   繁体   中英

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. 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:

<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

<?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. Here's a fiddle: http://jsfiddle.net/rrvQu/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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