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 ?

12th Apr 2019, 11:57 AM
eliot carbone
eliot carbone - avatar
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! 👍
13th Apr 2019, 6:58 AM
Ipang
+ 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.
12th Apr 2019, 8:20 PM
Ipang
+ 3
You're welcome, and good luck Eliot Carbone 👍
13th Apr 2019, 7:11 AM
Ipang
+ 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 !?
13th Apr 2019, 6:41 AM
eliot carbone
eliot carbone - avatar
+ 1
thank you ipang alot for the help 👍👍👍
13th Apr 2019, 7:09 AM
eliot carbone
eliot carbone - avatar