1
<form  method="post" action = "handler.php" > 
<input type="text" name="number1"  /> 
<input type="text" name="number2"  />
<input type="text" name="number3"  />
<input type="text" name="number4"  />
<input type="text" name="number5"  />
 etc..

I have a form set up like this to take in 10 different numbers. I want to add each of these numbers to an array so that I can sort the numbers. I know sort is a built in function but I want to write my own sorting algorithms I just need to get all of numbers into an array so I can pass it my functions.

$numArr = array ();

I have tried everything from array_push to call the $_POST['number1'] directly in the array itself. Every time I do echo $numArr all i get is an empty array as an output.

Shrp91
  • 173
  • 2
  • 5
  • 14
  • Make it easier by just using the form input name array syntax `` – mario Sep 28 '13 at 02:48
  • possible duplicate of [PHP creating an array from form input fields](http://stackoverflow.com/questions/12533044/php-creating-an-array-from-form-input-fields) – mario Sep 28 '13 at 02:49
  • Ok I didn't realize that was a viable way of doing it. Very new to PHP. And i had reviewed the other link you listed beforehand but I couldn't really follow it. – Shrp91 Sep 28 '13 at 02:56

2 Answers2

3

You have to use same name of input element and make it an array like,

<form method="post" action="handler.php"> 
  <input type="text" name="numbers[]" /> 
  <input type="text" name="numbers[]" />
  <input type="text" name="numbers[]" />
  <input type="text" name="numbers[]" />
  <input type="text" name="numbers[]" />
</form>

Now, When you submit your form on handler.php you will get an array of numbers[].

$_POST['numbers'];

You can sort array using sort().

$sort_array = sort($_POST['numbers']);

If you print $sort_array then you can see sorted array elements.

Manish Chauhan
  • 595
  • 2
  • 7
  • 14
0
<form method="post" action="handler.php"> 
  <input type="text" name="numbers[]" /> 
  <input type="text" name="numbers[]" />
  <input type="text" name="numbers[]" />
  <input type="text" name="numbers[]" />
  <input type="text" name="numbers[]" />
</form>

when submitted...

sort($_POST['numbers']);
djot
  • 2,952
  • 4
  • 19
  • 28
  • I know _sort_ is a built in function but I wanted to dabble into writing my own sorting algorithms I just needed a way to add all of the values into an array as their integer value. I tried $numArr = ($_POST['numbers']); but that didn't seem to work. – Shrp91 Sep 28 '13 at 02:55
  • `$numArr = $_POST['numbers'];` ...no parenthesis ... or use it directly. – djot Sep 28 '13 at 03:01
  • You don't have to declare $numArr as an array first? I just realized I forgot to put that in to my previous comment. It should be $numArr = array($_POST['numbers']); Which didn't work for me unless I am doing something else wrong. – Shrp91 Sep 28 '13 at 03:06
  • 1
    You should read a beginners guide to PHP and stop asking useless questions. – djot Sep 28 '13 at 03:13