duanao4729 2016-02-17 05:52
浏览 8
已采纳

如何在循环中提交没有提交按钮的选择菜单?

I have a code that submits a form when an option in a select box is clicked, but my problem is when I put it inside a loop, it doesn't work. Can you help me with it?

Here's the code:

<?php 
  $y1=5;
  for($x1=1; $x1<=$y1; $x1++){
?>
    <form name="myform[]" action="test.php" id ="myform" method="post">
      <select id="sel_id" name="sel_name[]"  onchange="submitform();">
      <?php 
        $y=5;
        for($x=1; $x<=$y; $x++){
      ?>
          <option value="<?php echo $x;?>"><?php echo $x;?></option>
      <?php
        }
      ?> 
      </select>
    </form>

    <script type="text/javascript">
      function submitform()
      {
        document.myform.submit();
      }
    </script>

<?php
  }
?>  

Now, I want every value of it be printed:

<?php
  echo $_POST['sel_name'];
?>
  • 写回答

4条回答 默认 最新

  • doulingna9420 2016-02-17 06:13
    关注

    You are using multiple form with same id="myform". this should be unique for every form.

    Example:

    <?php 
    $y1=5;
    For($x1=1; $x1<=$y1; $x1++){
    ?>
    <form name="myform[]" action="test.php" id ="myform_<?=$x1?>" method="post">
    <select id="sel_id" name="sel_name[]"  onchange="submitform(<?=$x1?>);">
    <?php 
    $y=5;
    For($x=1; $x<=$y; $x++){
    ?>
    <option value="<?php echo $x;?>"><?php echo $x;?></option>
    <?php
    }
    ?> 
    </select>
    </form>
    <?php
    }
    ?>  
    

    Your Java Script:

    <script type="text/javascript">
    function submitform(id)
    {
        document.getElementById("myform_"+id).submit();
    }
    </script>
    

    Your test.php:

    <?php
    if(isset($_POST['sel_name']))
        print_r($_POST['sel_name']);
    ?>
    

    I am using different form ids as myform_<?=$x1?> and also using this id into submitform() function.

    As per @Mr.Engineer, no need to use java script function submitform() inside the for() when you are using the unique IDs for form submission.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并