Array/Form help

Soldato
Joined
25 Jan 2003
Posts
11,542
Location
Newark, Notts
I've got the following code:

Code:
<?php
 $txt1 = $_POST['txt1'];
 $txt2 = $_POST['txt2'];
 $txt3 = $_POST['txt3'];
 $txt4 = $_POST['txt4'];
 $txt5 = $_POST['txt5'];
 $txt6 = $_POST['txt6'];
 $txt7 = $_POST['txt7'];
 $txt8 = $_POST['txt8'];
 $txt9 = $_POST['txt9'];
 $txt10 = $_POST['txt10'];
?>

<table><form name="form1" method="post" action="<?php echo $PHP_SELF;?>">
<tr><td><input type="text" name="names[]" id="txt1" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt2" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt3" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt4" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt5" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt6" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt7" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt8" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt9" /></td></tr>
<tr><td><input type="text" name="names[]" id="txt10" /></td></tr>
<tr><td><input type="submit" value="Submit" />
</form></table>
<?php

$names = array("","","","","","","","","","");
sort($names);
foreach ($names as $key => $val) {
   echo "names[" . $key . "] = " . $val . "\n";
}

?>

What I require the page to do is to put whatever values are typed into the form into the array and sort them in alphabetical order. The line below is the line i *think* is causing the problem:

$names = array("","","","","","","","","","");

If i type actual values in the speech marks then it sorts those values, but how can i get it to sort the values that are typed into the form? I thought it'd just be a case of typing txt1 txt2 etc but that doesn't work :confused:

Am i missing something somewhere?
 
nope, changed it to:

Code:
$names = array("$txt1","$txt2","$txt3","$txt4","$txt5","$txt6","$txt7","$txt8","$txt9","$txt10");

... and all it does is outputs:

Code:
names[0] = names[1] = names[2] = names[3] = names[4] = names[5] = names[6] = names[7] = names[8] = names[9] =

and doesn't change anything when i submit values entered into the text box's
 
Do'h should have seen it sooner. The name tag will be passed through post. So set name=txt1, name=txt2, get rid of the id tags and run it. Works for me.

Code:
<?php
 $txt1 = $_POST['txt1'];
 $txt2 = $_POST['txt2'];
 $txt3 = $_POST['txt3'];
 $txt4 = $_POST['txt4'];
 $txt5 = $_POST['txt5'];
 $txt6 = $_POST['txt6'];
 $txt7 = $_POST['txt7'];
 $txt8 = $_POST['txt8'];
 $txt9 = $_POST['txt9'];
 $txt10 = $_POST['txt10'];
?>

<table><form name="form1" method="post" action="<?php echo $PHP_SELF;?>">
<tr><td><input type="text" name="txt1" /></td></tr>
<tr><td><input type="text" name="txt2" /></td></tr>
<tr><td><input type="text" name="txt3" /></td></tr>
<tr><td><input type="text" name="txt4" /></td></tr>
<tr><td><input type="text" name="txt5" /></td></tr>
<tr><td><input type="text" name="txt6" /></td></tr>
<tr><td><input type="text" name="txt7" /></td></tr>
<tr><td><input type="text" name="txt8" /></td></tr>
<tr><td><input type="text" name="txt9" /></td></tr>
<tr><td><input type="text" name="txt10" /></td></tr>
<tr><td><input type="submit" value="Submit" />
</form></table>
<?php

$names = array("$txt1","$txt2","$txt3","$txt4","$txt5","$txt6","$txt7","$txt8","$txt9","$txt10");
sort($names);
foreach ($names as $key => $val) {
   echo "names[" . $key . "] = " . $val . "\n";
}

?>
 
Back
Top Bottom