PDA

View Full Version : Rewriting Form Fields



Strangeplant
09-17-2008, 07:30 PM
I'm totally missing something, and believe me when I say that I have tried.....

I have a check box in a form in a PHP script, and when checked, it needs to clear four other text input fields. So, I'm going to use javascript, but..... The form looks like this:
<form id="globe" name="aoiselect" method="post" action="<? echo $_SERVER['PHP_SELF'];?>">
<tr>
<td>Select AOI:
<select onchange="window.location='index.php?satellite='areaaoi='+this.value">
<?
for ($j = 0; $j <= $d2[$sat]; $j++) { // $j must be the # of elements in array group
?> <option value="<?=$j?>" <? if ($aoi == $j) { print "SELECTED";}?>> <?=$d1[$sat][$j][0]?></option><?} // write array to input fields below
?>
</select> or Define AOI:
<td>lat N: <input type="text" id="ordN" name="ordN" size="6" value="<?=$d1[$sat][$aoi][1] ?>">&nbsp;</td>
<td>lon E: <input type="text" id="ordE" name="ordE" size="6" value="<?=$d1[$sat][$aoi][2] ?>">&nbsp;</td>
<td>lat S: <input type="text" id="ordS" name="ordS" size="6" value="<?=$d1[$sat][$aoi][3]?>">&nbsp;</td>
<td>lon W: <input type="text" id="ordW" name="ordW" size="6" value="<?=$d1[$sat][$aoi][4]?>">&nbsp;</td>
<td>Whole Scan: <input type="checkbox" name="wholeScan" value="wholeScan" onFocus="setWholeScan(this)";></td>
</tr>
</form>
Then I have some javascript that goes with the onfocus:
function setWholeScan() {
if (document.aoiselect.wholeScan.checked) {
document.aoiselect.ordN.value = "";
document.aoiselect.ordE.value = "";
document.aoiselect.ordS.value = "";
document.aoiselect.ordW.value = "";
}
}
But, it doesn't work, nor any variation that I can think of..... So, what am I missing? I'm still building the program, and there is another, more troublesome problem to come, which I just might be able to solve if I can get this working. Please ignore the lack of submit button, and the post.....

mtran
09-17-2008, 08:52 PM
I'm wondering if it is because of onFocus="setWholeScan(this)" ?
How about trying onclick="setWholeScan();"

Twey
09-18-2008, 02:59 AM
<?php
function zip($arr1, $arr2) {
$r = array();

for ($i = 0, $n1 = count($arr1), $n2 = count($arr2); $i < $n1 && $i < $n2; ++$i)
$r[] = array($arr1[$i], $arr2[$i]);

return $r;
}

$lonlats = array(array('Lat', 'N'),
array('Lon', 'E'),
array('Lat', 'S'),
array('Lon', 'W'));
?>

<script type="text/javascript">
function setWholeScan(o) {
for (var e = o.form.elements, i = e.length; --i >= 0; )
if (e[i].name.substring(0, 3) === "ord")
e[i].value = e[i].defaultValue;
}
</script>

<form id="globe" name="aoiselect" method="post" action="<? echo $_SERVER['PHP_SELF'];?>">
<tr>
<td>
<label>
Select AOI:
<select onchange="location = 'index.php?satellite=&areaaoi=' + this.value;">
<?php for ($j = 0; $j <= $d2[$sat]; $j++) { ?>
<option value="<?php echo $j; ?>"
<?php if ($aoi === $j) echo 'selected'; ?>>
<?php echo $d1[$sat][$j][0]; ?>
</option>
<?php } ?>
</select>
</label>
or define AOI:
</td>
<?php foreach (zip($d1[$sat][$aoi], $lonlats) as $k => $v) ?>
<td>
<label>
<?php echo $v[1][0]; ?> <?php echo $v[1][1]; ?>:
<input type="text"
id="ord<?php echo $v[1][1]; ?>"
name="ord<?php echo $v[1][1]; ?>"
size="6"
value="<?php echo $v[0][$k + 1]; ?>">
<?php /* Are you sure about the above line? One-indexed? */ ?>
</label>
</td>
<?php } ?>
<td>
Whole Scan:
<input type="checkbox"
name="wholeScan"
value="wholeScan"
onfocus="setWholeScan(this);";>
</td>
</tr>
</form>

rangana
09-18-2008, 04:28 AM
I'm wondering if it is because of onFocus="setWholeScan(this)" ?
How about trying onclick="setWholeScan();"

It's fine. You might find reading this keyword (http://www.quirksmode.org/js/this.html) useful.

Strangeplant
09-19-2008, 01:11 PM
Twey, thanks for your post. There is still something wrong, however. The check box does not write ' ' to the four input fields, and this is my critical focus, how to read and write to in the input fields.

I added a missing left curly bracket at the end of the php foreach statement. The select AOI pull-down is populated correctly. Then it displays the line with the four input fields as expected, and labels them correctly, so the echos of the v$ array work for the field labels, elements [1][0] and [1][1].

I appreciate what you did for the array manipulation for the four latlon fields, and I tried to fix it by changing the index from $k+1 to $k. It is a nice way to do that with everything associated. - Thanks for the structure tip. However, the returned array elements contain only one character because the calling argument $d1[$sat][$aoi] is treated as a string, and subsequent array work in zip() pulls the string apart in its characters, one at a time.