duanliang2017 2013-10-08 12:25
浏览 170
已采纳

PHP在循环中获取多个输入文本框的值

I used Javascript with PHP. In my HTML form it has a drop down(option) menu so that user can select the no. of authors. According to the number selected, dynamic input boxes will be displayed under names of authors. So here is my HTML form code.

<form method="post" action="" onsubmit="">
No. of Authors:<select id="steps_number" name="AuthorNo">
                  <option value="1" selected="selected" >1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                </select><br/>
Author Names:<div id="inputBoxes"></div><br/>
<button type="submit" name="submit"></form>

My javascript code to display dynamic input text boxes according to author number selected is;

<script language="javascript">
    $(document).ready(function(){
        $("#steps_number").change(function() { 
            var inputNum = $("#steps_number").val();

            var inputCode = "";
            for(i=0; i<inputNum; i++) { 
                inputCode += "<p class=\"inputBox\"><input name='inp' id=\"inputBox_" + i + "\" size=\"20\" type=\"text\"></p>"
            }
            $("#inputBoxes").html(inputCode);
        });
    });
</script>

PHP code to get values when submitted is;

<?php
if(isset($_POST['submit'])) { 
$AuthorNo=strip_tags($_POST['AuthorNo']);
$AName=strip_tags($_POST['inp']);}

If the no of authors are selected as 2, two input text boxes will be displayed. If i submit it with two different values in each input box, only the value of 1st input box will be inserted to the database. Please tell me a way to name the input boxes in the loop differently and a way to call their values? Thank You.

  • 写回答

1条回答 默认 最新

  • duanguochi6194 2013-10-08 12:27
    关注

    The input can have a name like name=inpAuthor[] this will send an array to PHP.

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条