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

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题