-2

I want to store the session data into a session variable so that it can be used in another php page other than the action="addbill.php" in form.But the problem is i want to store the array name[] into the session variable but shows error.

<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>

Output Error:Undefined index: name

"ANY HELP IS WELCOMED"

NR17
  • 7
  • 5
  • Is there a reason you need it to be an array – fraggley May 31 '20 at 15:00
  • Yes to send it into SQL database, before doing some calculations – NR17 May 31 '20 at 15:01
  • `addInput()` < where and what does that JS function do? – Funk Forty Niner May 31 '20 at 15:01
  • JS function dyanmically adds input field inaccordance with the user, when he clicks add another text input – NR17 May 31 '20 at 15:02
  • @NitishRoat you don't need a variable to be an array to put it into an SQL database. The clarification is needed as there's an assumption that there will be multiple name[] inputs (hence you need it to be an array) but then you wouldn't add it as a session variable – fraggley May 31 '20 at 15:05
  • If i have 5 or more { dynamic } different input fields with name="name", and i want all names in another php file then, without an array is it possible?? Other than the action="addbill.php" file. – NR17 May 31 '20 at 15:08

1 Answers1

0

Just change your form input name to be a standard variable, not an array:

<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>
fraggley
  • 1,215
  • 2
  • 9
  • 19