cpsc_girl Posted November 17, 2006 Share Posted November 17, 2006 I was trying to create a form that uses text boxes to input numeric values, then add the values to an array. Then print a statement with the sorted values, but i'm completely lost. I created the form which i believe is right: <html><head><title>Average and Median values</title></head><body><h1>Value Finder</h1><font size=4><p>Please enter up to 8 numbers in the form below.</p><form action="array_form.php" method="post">Value 1 : <input type="text" size="5" maxlength="10" name="value"><br>Value 2 : <input type="text" size="5" maxlength="10" name="value"><br>Value 3 : <input type="text" size="5" maxlength="10" name="value"><br>Value 4 : <input type="text" size="5" maxlength="10" name="value"><br>Value 5 : <input type="text" size="5" maxlength="10" name="value"><br>Value 6 : <input type="text" size="5" maxlength="10" name="value"><br>Value 7 : <input type="text" size="5" maxlength="10" name="value"><br>Value 8 : <input type="text" size="5" maxlength="10" name="value"><br><br><input type="submit" value="Submit"><input type="reset" value="Clear"></font></form></body></html>The part that i'm lost on is how to get the numbers in the array from the form. The book i'm using to teach myself is no help at all. Someone please help me. On the server that i'm using the registerd globlas are turned off. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 Try changing the name to "value[]", and then look in $_POST['value'] for the array..If you don't add the "[]", php will only give you the last value entered, not all of them (though you can get them all by parsing the POST data manually) Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 17, 2006 Author Share Posted November 17, 2006 So the code should look like this instead, i was sure if i had to include the brackets or not. <html><head><title>Average and Median values</title></head><body><h1>Value Finder</h1><font size=4><p>Please enter up to 8 numbers in the form below.</p><form action="array_form.php" method="post">Value 1 : <input type="text" size="5" maxlength="10" name="value[]">Value 2 : <input type="text" size="5" maxlength="10" name="value[]">Value 3 : <input type="text" size="5" maxlength="10" name="value[]">Value 4 : <input type="text" size="5" maxlength="10" name="value[]">Value 5 : <input type="text" size="5" maxlength="10" name="value[]">Value 6 : <input type="text" size="5" maxlength="10" name="value[]">Value 7 : <input type="text" size="5" maxlength="10" name="value[]">Value 8 : <input type="text" size="5" maxlength="10" name="value[]"><br><br><input type="submit" value="Submit"><input type="reset" value="Clear"></font></form></body></html>I'm still stuck on creating an array from the numbers input in the form, for some reason i can't get the array to work at all.<?php $value = $_POST["value"]; $numb = array($value);print ("$numb"); ?>I couldn't find any examples comparative to this particular problem. Quote Link to comment Share on other sites More sharing options...
Adika Posted November 17, 2006 Share Posted November 17, 2006 Hi cpsc_girl.Your form part is good. Here is the php part:[code]<?php $value = $_POST["value"]; foreach($value as $numb) { print ("$numb"); }?>[/code] Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 17, 2006 Author Share Posted November 17, 2006 Thanks. I tried the php code you just posted but i'm still receiving an error: Warning: Invalid argument supplied for foreach() in /usr/local/www/cpsc/students/thamp815/array_form.php on line 5Also what happens to the array isn't that required if you're trying to create an array? Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2006 Share Posted November 17, 2006 Yu dont need to define an array as the form creates an array for you when yoiu use the value[] method.Your form. You need to move your </font> tag to be after your </form> tag. The rest is fine.The rest of your code should be working fine though you will get the above metntioned error if none of your form fileds have been filled in. Try...[code]<?phpif (isset($_POST['value'] && is_array($_POST['value'])) { foreach($_POST['value'] as $numb) { echo $numb; }} else {// do some debugging.print_r($_POST);}?>[/code] Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 17, 2006 Author Share Posted November 17, 2006 The code works fine now, but all i wanted it to do was just to list all the numbers that were input from the form. I already know how to sort the numbers once i have them. Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2006 Share Posted November 17, 2006 [quote]all i wanted it to do was just to list all the numbers that were input from the form[/quote]That is all that code does. Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 17, 2006 Author Share Posted November 17, 2006 When i run the code this is what i get instead of a list: Array ( [value] => 1588 ) Quote Link to comment Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 There is a slight syntax error in my code, but other than that its working fine here. Please post your code. Just the php part should suffice. Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 18, 2006 Author Share Posted November 18, 2006 [code]<?php $value = $_POST["value"]; if (isset($_POST['value']) && is_array($_POST['value'])) { foreach($_POST['value'] as $numb) { echo $numb; }} else {// do some debugging.print_r($_POST);}?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 Did you fix your form as I said? You need to move your </font> tag to be after your </form> tag. That is all I can think of (and I doupt is the problem) as its working fine here for me. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 18, 2006 Share Posted November 18, 2006 Then the conditions in the if statement are not getting set and you are seeing the post data in the else branch. There must be a mistake somewhere.Copy and paste this entire code into a page and it should work for you:[code]<html><head><title>Average and Median values</title></head><body><?phpif (isset($_POST['value'])) { echo "<pre>"; print_r($_POST[value]); echo "</pre>";}?><h1>Value Finder</h1><font size=4><p>Please enter up to 8 numbers in the form below.</p><form action="<?=$_SERVER['PHP_SELF']?>" method="post">Value 1 : <input type="text" size="5" maxlength="10" name="value[]"><br>Value 2 : <input type="text" size="5" maxlength="10" name="value[]"><br>Value 3 : <input type="text" size="5" maxlength="10" name="value[]"><br>Value 4 : <input type="text" size="5" maxlength="10" name="value[]"><br>Value 5 : <input type="text" size="5" maxlength="10" name="value[]"><br>Value 6 : <input type="text" size="5" maxlength="10" name="value[]"><br>Value 7 : <input type="text" size="5" maxlength="10" name="value[]"><br>Value 8 : <input type="text" size="5" maxlength="10" name="value[]"><br><input type="submit" value="Submit"><input type="reset" value="Clear"></form></font></body></html>[/code]EDIT: It also works with the font tag out of place, so the error is somewhere else. Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 18, 2006 Author Share Posted November 18, 2006 Yes i did fix the problem with my form as you said. Did i correct the right syntax that you mentioned before. Is there another way to write the code? I did not think that i would have this much trouble on something as simple as arrays :'( Quote Link to comment Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 There are easier ways of writting your form without using arrays. Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 18, 2006 Author Share Posted November 18, 2006 The chapter i was studying is all about arrays which is why i'm trying to use an array. I didn't think that teaching myself would be this hard. Maybe it would make more since to post the complete problem that way you could understand completely what i was trying to do. Quote Link to comment Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 Maybe, but as it stands, there is nothing wrong with the code you posted earlier. Its working 100% as expected on my machines. Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 18, 2006 Author Share Posted November 18, 2006 Well i guess i'm stuck then i wonder why if works for you but not for me. Is there a simplier way to write an array? Quote Link to comment Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 [quote]Is there a simplier way to write an array?[/quote]Of course, but it really depends what you want to use it for. A few examples...[code]<?php $arrayofnames = array('bob','betty','bill'); foreach($arrayofnames as $name) { echo $name.'<br />'; }?>[/code]or...[code]<?php $arrayofnumber = array(1,5,66,33,10); foreach($arrayofnumbers as $number) { echo $number.'<br />'; }?>[/code] Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 18, 2006 Author Share Posted November 18, 2006 I know how to do an array like in your examples but i was having trouble doing one from a form where you input numeric values. Quote Link to comment Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 [quote]I know how to do an array like in your examples but i was having trouble doing one from a form where you input numeric values.[/quote]Well Im at a loss, your other code should be working. Quote Link to comment Share on other sites More sharing options...
cpsc_girl Posted November 18, 2006 Author Share Posted November 18, 2006 Well thank you so much for all your help i really appreciate it. Maybe if i take a break and come back to it i'll be able to figure it out, atleast long enough to get rid of my headache :P. Quote Link to comment Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 Hehe, sorry I couldn't be of more help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.