Forum Moderators: coopster

Message Too Old, No Replies

Problem with dynamically setting html checkbox's with php.

         

nelsonm

7:51 pm on Nov 17, 2010 (gmt 0)

10+ Year Member



I can figure out what is wrong with this php/html code.

I'm trying to get the check boxes in the html code to retain their previous selections every time the user clicks on submit but new boxes get checked even when i don't alter the boxes every time i click on submit.

Also, when the php script is run the very first time, i get extraneous boxes checked that i did not ask for.

You can test the code yourself by cutting and pasting it into you system. It is self contained.

Any help would be greatly appreciated.



<?php
// Set error handling.
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('track_errors', true);

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$clientIP = $_SERVER['REMOTE_ADDR'];
}


unset($aom, $aomx);
$zipSubmit = $_POST['Submit'];

if (isset($_POST['Submit'])){
$aom= array("0","0","0","0","0","0","0","0","0","0","0","0","0","0");
$aomx= $_POST['aom'];
$zipMilesHigh= $_POST['zipMilesHigh'];
$zip1= $_POST['zip1'];

if (!empty($aomx)){foreach ( $aomx as $id => $val) $aom[$id] = $val; }else{ $aom = array("1","0","0","0","0","0","0","0","0","0","0","0","0","0"); }
//foreach ( $aom as $id => $val) echo " aom[".$id."]=".$val;
echo "<br>** Submit set: ";

}else{
$aom = array("1","0","0","0","0","0","0","0","0","0","0","0","0","0");
$zipMilesHigh= "20";
$zip1= "48207";

echo "<br>** Submit not set: ";
}

echo $zipSubmit.", aom["; if (!empty($aom)){ foreach ($aom as $value) echo $value; }else{ echo "x000000000000x"; } echo "], miles: ".$zipMilesHigh.", zip1: ".$zip1;
echo "<br>** ";
var_dump($aom);
echo "<br>";
?>

<!-- html input form. -->
<html>
<body>

<table border="0" align="center" cellpadding="20" cellspacing="0" style="border:1px solid #FFB219;">
<td><b>Find charities offering selected Act of Mercy within selected miles of specified zip code</b></td>
</table>

<form action="" method="post">
<table border="1" align="center" cellpadding="10" cellspacing="0" style="border:1px solid #FFB219;">
<br><br>
<tr>
<td>Find all Charities with the selected Act(s) of Mercy<br>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[00] == "1") echo ' checked="checked"'; ?>/> Feed the hungry<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[01] == "1") echo ' checked="checked"'; ?>/> Give drink to the thirsty<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[02] == "1") echo ' checked="checked"'; ?>/> Clothe the naked<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[03] == "1") echo ' checked="checked"'; ?>/> Visit the and ransom the captive<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[04] == "1") echo ' checked="checked"'; ?>/> Shelter the homeless<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[05] == "1") echo ' checked="checked"'; ?>/> Visit the sick<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[06] == "1") echo ' checked="checked"'; ?>/> Bury the dead<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[07] == "1") echo ' checked="checked"'; ?>/> Instruct the ignorant<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[08] == "1") echo ' checked="checked"'; ?>/> Counsel the doubtful<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[09] == "1") echo ' checked="checked"'; ?>/> Admonish sinners<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[10] == "1") echo ' checked="checked"'; ?>/> Bear wrongs patiently<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[11] == "1") echo ' checked="checked"'; ?>/> Forgive offenses willingly<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[12] == "1") echo ' checked="checked"'; ?>/> Comfort the afflicted<br/>
<input name="aom[]" type="checkbox" value="1"<?php if ($aom[13] == "1") echo ' checked="checked"'; ?>/> Pray for the living and the dead<br/>
</td>
<td>within:
<select name="zipMilesHigh" id="zipMilesHigh">
<option value="05"<?php if ($zipMilesHigh == 05) echo ' selected="selected"'; ?>> 5 Miles</option>
<option value="10"<?php if ($zipMilesHigh == 10) echo ' selected="selected"'; ?>>10 Miles</option>
<option value="15"<?php if ($zipMilesHigh == 15) echo ' selected="selected"'; ?>>15 Miles</option>
<option value="20"<?php if ($zipMilesHigh == 20) echo ' selected="selected"'; ?>>20 Miles</option>
<option value="25"<?php if ($zipMilesHigh == 25) echo ' selected="selected"'; ?>>25 Miles</option>
<option value="30"<?php if ($zipMilesHigh == 30) echo ' selected="selected"'; ?>>30 Miles</option>
<option value="35"<?php if ($zipMilesHigh == 35) echo ' selected="selected"'; ?>>35 Miles</option>
<option value="40"<?php if ($zipMilesHigh == 40) echo ' selected="selected"'; ?>>40 Miles</option>
<option value="45"<?php if ($zipMilesHigh == 45) echo ' selected="selected"'; ?>>45 Miles</option>
<option value="50"<?php if ($zipMilesHigh == 50) echo ' selected="selected"'; ?>>50 Miles</option>
</select> of ZIP Code: <input name="zip1" type="text" size="10" maxlength="10" value=<?php echo '"'.$zip1.'"'; ?> onfocus='if(this.value==<?php echo '"'.$zip1.'"'; ?>)this.value=""'/>
</td>
<td><input type="submit" name="Submit" value="Submit">&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

<?php
echo $zipSubmit.", aom["; if (!empty($aom)){ foreach ($aom as $value) echo $value; }else{ echo "x000000000000x"; } echo "], miles: ".$zipMilesHigh.", zip1: ".$zip1;
echo "<br>** ";
var_dump($aom);
echo "<br>";
?>

enigma1

10:51 pm on Nov 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you setup the integers for the array keys properly?

eg:

not
$aom[00]
$aom[01]

but
$aom[0]
$aom[1]

etc.

nelsonm

11:35 pm on Nov 17, 2010 (gmt 0)

10+ Year Member



Done and thanks...

Ok, i would not have guessed that leading zeros in the index of an array would make a difference, but it does?

The only problem now is that when you click on the submit button with whatever boxes checked, all checked boxes other than the first shift up.

Any clues on that one?

nelsonm

1:16 am on Nov 18, 2010 (gmt 0)

10+ Year Member



Ok, i figured out the second problem. I have to enumerate the checkbox input name array...

from
name=aom[]
name=aom[]
name=aom[]
.
.
.


to
name=aom[0]
name=aom[1]
name=aom[2]
.
.
.

because like i read before, if you leave the indexes blank, the values will be inserted sequentially in the array instead of into their correct index.

Thanks for pointing me in the right direction.

enigma1

9:32 am on Nov 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



leading zeros in the index of an array would make a difference

PHP thinks is not an integer, so it maps 00 like you had '00' as a string, unless there is something defined in which case it will use the definition.

nelsonm

7:14 pm on Nov 18, 2010 (gmt 0)

10+ Year Member



thanks a lot on the clarification!