2 Replies - 275 Views - Last Post: 13 July 2013 - 06:14 AM Rate Topic: -----

#1 Astro52a9  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 24-March 13

Add to Array from a form

Posted 13 July 2013 - 04:04 AM

I have this simple text code that i want to get the input from the user and add it to my array but it only holds one and keep replacing it
I am a newbie so


<?php 

$name_array = array();

array_push ($name_array,$_GET['name']);

print_r($name_array);

?>


<form action="formtest.php" method="GET">
	Name<input type="text" name="name"><br>
	<br>
	<input type="submit" value="Submit"><br>
	</form>




thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Add to Array from a form

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,139
  • Joined: 08-August 08

Re: Add to Array from a form

Posted 13 July 2013 - 04:40 AM

Your form only submits one element. How many do you hope to add?
HTML is processed on the browser and PHP is processed on the server. Two different computers and at two different times. Putting them in the same file dos not change this fact.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3359
  • View blog
  • Posts: 11,373
  • Joined: 12-December 12

Re: Add to Array from a form

Posted 13 July 2013 - 06:14 AM

.. each time your form is submitted, the page is re-loaded. This runs your script entirely from scratch, so the array $name_array is re-created each time.

If you want to store any previous values you'll need to persist them by saving them in a database. Or, if appropriate, in a cookie or session-data (or some other technology).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1