0
Is this the right wat to add data into array with a form?
Im trying to add elements to an empty array in php with a form and in the same time to be able to see the elements in the same page in the browser .The way im doing this is in this way: <form action="index.php" method="POST"> <input type="text" name="name"> <input type="submit" name="submit" value="submit"> </form> <?php if(isset($_POST['submit'])){ $name=$_POST['name']; $people=array( ); $names=array_push($people, $name); for($i=0 ; $i < count($names) ; $i ++){ echo $names[$i]; }; ?> Now is that the right way ?
5 Réponses
+ 3
Have you got to "Predefined Variables" chapter? in that you'll find "$_SESSION" subchapter, where they cover session variables and how to use them. I encourage you to keep at the lesson, when you get to that chapter you'll get the idea.
Good luck! 👍
+ 4
I'm not telling you right or wrong, but as I look at it, I think you will have only the last name submitted. Each time the form is submitted the array is created (with no elements) and the submitted name is added to it.
If you're really curious about this, I'd probably suggest to make the array as a variable in session. I still don't understand where you're going with this idea though, so I'll leave the choice to you.
+ 3
You're welcome, and good luck Eliot Carbone 👍
+ 1
Yes thats what indeed happens , everytime i add a new element in the input field , it overwrites the last one . And i want to be able to add all the elements into the empty array , and display them into the browser . I kind of a newbe in the Php field how do i do that !?
+ 1
thank you ipang alot for the help 👍👍👍