首页 > 解决方案 > 如果表单名称=数组,如何将 $_POST 数据存储到会话变量中?

问题描述

我想将会话数据存储到会话变量中,以便它可以在表单中的 action="addbill.php" 之外的另一个 php 页面中使用。但问题是我想将数组名称 [] 存储到会话中变量但显示错误。

<form action="addbill.php" method="POST">
     <div id="dynamicInput">
     <br><input type="text" name="name[]">
     </div>
     <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
     <input type="submit"/>
</form>
<?php
 session_start();
 $_SESSION['addedmem']= $_POST['name'];
 var_dump($_SESSION['addedmem']);
?>
<script>
                var counter = 1;
                var limit = 4;
                function addInput(divName){
                     if (counter == limit)  {
                          alert("You have reached the limit of adding " + counter + " inputs");
                     }
                     else {
                          var newdiv = document.createElement('div');
                          newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
                          document.getElementById(divName).appendChild(newdiv);
                          counter++;
                     }
                }
</script>

输出错误:未定义索引:名称

“欢迎任何帮助”

标签: phpsession

解决方案


只需将表单输入名称更改为标准变量,而不是数组:

<form action="addbill.php" method="POST">
     <div id="dynamicInput">
     <br><input type="text" name="name">
     </div>
     <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
     <input type="submit"/>
</form>

推荐阅读