在打印方法中获取单选值,以便将某些参数转移到另一页


get radio value in printing method in order to transfer some parameters to another page

我有这段PHP代码:

 <form action="modificaPriorita.php" method="post">
   <table>
     <tr id="format-tabelle">
        <th></th>
        <th><span class="format-celle">TITLE</span></th>
        <th><span class="format-celle">TYPE</span></th>
        </tr>
        <?php    
          $res = GestioneSegnalazione::showSegnalazioni();
          function drawTable($res) {
            $title = $res[0];
            $idAuthor = $res[1];
            $type = $res[2];
            for ($i = 0; $i < $num; $i++) {
               print "<tr class='row'>
               <td align='center' valign='middle'> <input type='radio' name='radio' value='$title[$i],$idAuthor[$i]'/> </td>
               <td align='center' valign='middle'><span class='format-celle'> $title[$i]</span></td>
               <td align='center' valign='middle'><span class='format-celle'> $type[$i]</span></td>
      ?>
     </table>
     <br>
     <input type="submit" name="show" value="Show" class='freshbutton-orange'  id='show-submit-segnalazione'/>
       <?
         if (isset($_POST['show'])) {
           if ($_POST[radio] == "") {
echo("<SCRIPT JavaScript'>window.alert('Select one segnalazione to show');window.location.href='showSegnalazione.php#close'</SCRIPT>");
           }
         }
 ?>

如果我选择一个项目在单选按钮,我按下按钮'显示'它的工作,它去另一个页面"modificaPriorita.php"有两个参数:标题,id作者我的问题是,如果我按下"show"按钮而不选择单选按钮中的一个项目,它会向我显示错误"选择一个信号azione显示",但它会转到没有参数的新页面("modificaPriorita.php")。我该怎么做才能得到这个错误信息?

您不需要等待表单发布后才可以检查他们是否选择了一个单选按钮。您可以像这样对onsubmit侦听器使用javascript:

<form action="modificaPriorita.php" method="post" onsubmit="return validateForm();">
    <table>
        <tr id="format-tabelle">
            <th></th>
            <th><span class="format-celle">TITLE</span></th>
            <th><span class="format-celle">TYPE</span></th>
        </tr>
<?php    
    $res = GestioneSegnalazione::showSegnalazioni();
    function drawTable($res) {
        $title = $res[0];
        $idAuthor = $res[1];
        $type = $res[2];
        for ($i = 0; $i < $num; $i++) {
            print "<tr class='row'>
                <td align='center' valign='middle'> <input type='radio' name='radio' value='$title[$i],$idAuthor[$i]'/> </td>
                <td align='center' valign='middle'><span class='format-celle'> $title[$i]</span></td>
                <td align='center' valign='middle'><span class='format-celle'> $type[$i]</span></td>
             </tr>";
       }
   }
?>
    </table>
    <br>
    <input type="submit" name="show" value="Show" class='freshbutton-orange'  id='show-submit-segnalazione'/>
</form>
<script>
    function validateForm() {
        var radios = document.getElementsByName('radio');
        var checked = false;
        for (var i = 0, length = radios.length; i < length; i++) {
            if (radios[i].checked) {
                checked = true;
                break;
            }
        }
        if (checked) {
            return true;            
        } else {
            alert('Select one segnalazione to show');
            return false;
        }
    }
</script>

因此,这段代码中发生的事情是在提交表单之前运行我们设置的validateForm()函数。在该函数中,它检查是否选中了单选按钮。如果是,则该函数返回true,允许表单发布。如果没有找到选中的单选按钮,它会打开警告窗口,然后返回false,阻止表单发送。