ms1990kl Posted January 28, 2009 Share Posted January 28, 2009 Can somebody tell me why the following doesn't work. This is hugely simplified from the website. Basically, I have 4 input boxes, with the input name being an array fix[]. When I GET these inputs fix[0], fix[1], etc., they are fine. But when I try to use fix[1], fix[2], etc. in another array, it fails. The code is very simple and is below. Thanks in advance for your help. MIchael Smith <?php $testArray = array(10,20,30,40); if(array_key_exists('fixGetter', $_GET)) { if (isset($_GET['fix'])) { for ($i=0; $i<=count($_GET['fix'])-1; $i++) { echo $_GET['fix'][$i]; echo $testArray[$_GET['fix'][$i]]; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET"> <p class="input"><input name="fix[]" type="checkbox" value=" <?php echo 1; ?> "> </p> <p class="input"><input name="fix[]" type="checkbox" value=" <?php echo 2; ?> "> </p> <p class="input"><input name="fix[]" type="checkbox" value=" <?php echo 3; ?> "> </p> <p class="input"><input name="fix[]" type="checkbox" value=" <?php echo 4; ?> "> </p> <input type="submit" name="fixGetter" id="fixGetter" value="Calculate"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/142869-solved-inputted-array/ Share on other sites More sharing options...
DarkWater Posted January 28, 2009 Share Posted January 28, 2009 Any errors? And by the way, you should use foreach loops instead of a for loop with count(). Link to comment https://forums.phpfreaks.com/topic/142869-solved-inputted-array/#findComment-748976 Share on other sites More sharing options...
ms1990kl Posted January 28, 2009 Author Share Posted January 28, 2009 Yes, This is the output when I click on boxes 1 and 2 1 Notice: Undefined index: 1 in C:\Apache Sites\SmithsofWeston\formtest.php on line 9 2 Notice: Undefined index: 2 in C:\Apache Sites\SmithsofWeston\formtest.php on line 9 Link to comment https://forums.phpfreaks.com/topic/142869-solved-inputted-array/#findComment-748977 Share on other sites More sharing options...
DarkWater Posted January 28, 2009 Share Posted January 28, 2009 You really should use a foreach loop for this. <?php $_GET['fix'] = array(1, 3); //test values $_GET['fixGetter'] = true; //test values $testArray = array(10,20,30,40); if($_GET['fixGetter']) { if (isset($_GET['fix'])) { foreach($_GET['fix'] as $val) { printf("GET: %d; \n Test: %d\n", $val, $testArray[$val]); } } } ?> Link to comment https://forums.phpfreaks.com/topic/142869-solved-inputted-array/#findComment-748994 Share on other sites More sharing options...
ms1990kl Posted January 29, 2009 Author Share Posted January 29, 2009 I tried your code, but the same problem persists. I did comment out the 1st 2 lines. I also altered the array to make it $testArray = array(1=>10,20,30,40); $val comes out fine, but $testArray[$val] doesn't work. This is the output when I click on buttons 2 and 4. Notice: Undefined index: 2 in C:\Apache Sites\SmithsofWeston\formtest2.php on line 9 GET: 2; Test: 0 Notice: Undefined index: 4 in C:\Apache Sites\SmithsofWeston\formtest2.php on line 9 GET: 4; Test: 0 Thanks, Mike Link to comment https://forums.phpfreaks.com/topic/142869-solved-inputted-array/#findComment-749094 Share on other sites More sharing options...
DarkWater Posted January 29, 2009 Share Posted January 29, 2009 Ah, I found the problem. You have a space before and after the number in your form. Link to comment https://forums.phpfreaks.com/topic/142869-solved-inputted-array/#findComment-749100 Share on other sites More sharing options...
ms1990kl Posted January 29, 2009 Author Share Posted January 29, 2009 Holy Cow, That solved the problem. Thanks so much. I spent hours trying to fix this silly thing. Also, thanks for the tip on the foreach loop. Case closed. Michael Smith Link to comment https://forums.phpfreaks.com/topic/142869-solved-inputted-array/#findComment-749108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.