Results 1 to 5 of 5

Thread: Rewriting Form Fields

  1. #1
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default Rewriting Form Fields

    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:
    Code:
                        <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:
    Code:
        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.....

  2. #2
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I'm wondering if it is because of onFocus="setWholeScan(this)" ?
    How about trying onclick="setWholeScan();"

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <?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>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. The Following User Says Thank You to Twey For This Useful Post:

    Strangeplant (09-19-2008)

  5. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Quote Originally Posted by mtran View Post
    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 useful.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. The Following User Says Thank You to rangana For This Useful Post:

    Strangeplant (09-19-2008)

  7. #5
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    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.
    Last edited by Strangeplant; 09-19-2008 at 01:45 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •