dongpo1599 2013-06-10 12:19
浏览 54
已采纳

在打印方法中获取无线电值,以便将一些参数传输到另一个页面

I have this code of 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>");
           }
         }
 ?>

If i select one item in radio button and i press the button 'show' it works, it goes to the other page "modificaPriorita.php" with two parameters: title, idAuthor My problem is if i press the 'show' button without select one item in radio button, it would show me the error 'Select one segnalazione to show', but it goes to the new page ("modificaPriorita.php") without parameters. what can i do to have this error message?

  • 写回答

1条回答 默认 最新

  • dongpo5264 2013-06-10 13:19
    关注

    You don't need to wait for your form to post before you can check to see if they selected a radio button. You can use javascript with the onsubmit listener like so:

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

    So what happens in this code is right before the form gets submitted runs the validateForm() function we set up. In that function it checks to see if a radio button is selected. If it is then the function returns true which allows the form to post. If it doesn't find a radio button selected it does the alert window and then returns false which prevents the form from posting. I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义