2011-11-11 91 views
0

我想让用户选择一个“行”用于提交请求报告类型。如何将单选按钮放在表格的第一列中,并且选择哪一个是通过提交按钮发送到下一页的活动行?格式化HTML表单元素

回答

4

我认为安德烈亚斯是在正确的轨道上,但它没有那么有用,因为它可能是。这应该是一个好一点:

<?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; 

表单处理程序:

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

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

如果你想这样做的纯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

<?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 .. 
    } 
?> 

但是,如果你愿意使用一些jQuery的,你可以将textfields命名为相同的东西,并禁用你不打算使用的字段。这里是一个小提琴:http://jsfiddle.net/rrvQu/1/