Jump to content

[SOLVED] inputted array


ms1990kl

Recommended Posts

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

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]);
}
   }
}
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.