HTML张贴数组只张贴第一个值

HTML Posting An Array Only Posting First Value

本文关键字:张贴 第一个 数组 HTML      更新时间:2023-09-26

这是我的代码

<!DOCTYPE html>
<html>
<head>
  <title>Certificate Generator</title>
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
  <style type="text/css">
  <!--
    #main {
      max-width: 800px;
      margin: 0 auto;
    }
    #main .my-form form .text-box .add-box strong {
      font-size: 36px;
    }
-->
  </style>
</head>
<body>
  <div id="main">
    <h1 align="center">Certificate Generator</h1>
    <div class="type">
    <form name="class" action="generate.php" method="post">
  <input type="checkbox" name="class" value="i">IL<br>
  <input type="checkbox" name="class" value="u">UT</br> 
  </div>
    <div class="my-form">
        <form action ="generate.php"role="form" method="post">
             <label for="text">Date <span class="box-number"</span></label>
             <input type="text" name="date" /></p>
            <p class="text-box"> 
                <label for="box1">Student <span class="box-number">1</span></label>
                <input type="text" name="students[]" value="" id="box1" />
                <a class="add-box" href="#"><strong>Add More Students</strong></a>
            </p>
            <p>
              <input type="submit" value="Generate Certificates" />
            </p>
        </form>
    </div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
    $('.my-form .add-box').click(function(){
        var n = $('.text-box').length + 1;
        if( 250 < n ) {
            alert('Maximum Amount of Students');
            return false;
        }
        var box_html = $('<p class="text-box"><label for="box' + n + '">Student <span class="box-number">' + n + '</span></label> <input type="text" name="students[]" value="" id="box' + n + '" /> <a href="#" class="remove-box">Remove</a></p>');
        box_html.hide();
        $('.my-form p.text-box:last').after(box_html);
        box_html.fadeIn('slow');
        return false;
    });
    $('.my-form').on('click', '.remove-box', function(){
        $(this).parent().css( 'background-color', '#FF6C6C' );
        $(this).parent().fadeOut("slow", function() {
            $(this).remove();
            $('.box-number').each(function(index){
                $(this).text( index + 1 );
            });
        });
        return false;
    });
});
</script>
</body>
</html>

点击提交时,php只获取数组的第一个值

$students = ($_POST["students"]);

这是我的php表单,我不知道为什么它只返回数组的第一个值,这是我第一次在JavaScript中使用表单所以这可能只是一个简单的错误,我不确定

这就是你的问题:

  <form name="class" action="generate.php" method="post">
    <input type="checkbox" name="class" value="i">IL<br>
    <input type="checkbox" name="class" value="u">UT</br>
</div>
<div class="my-form">
  <form action ="generate.php"role="form" method="post">

由于关闭</div>,您正在过早地关闭您的表单,然后打开一个新表单。

你的html是无效的,浏览器试图使它有意义,导致你的students[]元素是你的表单之外,所以他们永远不会被张贴。

结果可能因浏览器而异(在Firefox中测试),但是验证您的html应该可以解决您的问题。

好了,我找到了错误。我有这个工作。您没有在表单中添加新元素。我将HTML重新排列如下:

<form name="class" action="generate.php" method="post">
<div id="main">
<h1 align="center">Certificate Generator</h1>
<div class="type">
  <input type="checkbox" name="class" value="i">IL<br>
  <input type="checkbox" name="class" value="u">UT</br>
</div>
<div class="my-form">
<form action ="generate.php"role="form" method="post">
  <label for="text">Date <span class="box-number"</span></label>
  <input type="text" name="date" />
  </p>
  <p class="text-box">
    <label for="box1">Student <span class="box-number">1</span></label>
    <input type="text" name="students[]" value="" id="box1" />
    <a class="add-box" href="#"><strong>Add More Students</strong></a> </p>
  <p>
    <input type="submit" value="Generate Certificates" />
  </p>
  </div>
  </div>
</form>
为了澄清,我将表单的开始标签移到了"main"div之外,然后在"main"div的结束标签之后关闭了表单。无其他改动