Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Changing PHP form array values using JavaScript

    Say I have the following PHP code:
    PHP Code:
    <form name="value_list" method="POST" action="value_lister.php">
    <?php
    foreach ($somevar as $k=>$v) {
        
    ?>
        <tr>
            <td>
                <input name="form_var[<?=$k ?>][name]" type="text" size="40" value="<?=$v['name'?>" />
            </td>
            <td>
                <input name="form_var[<?=$k ?>][number]" type="text" size="8" value="<?=$v['number'?>" />
            </td>
            <td>
                <select name="form_var[<?=$k ?>][type]">
                    <option value="static" onClick="document.value_list.form_var[<?=$k ?>][number].disabled=false"<?=($v['type']=='static'?' selected':''?>>Static</option>
                    <option value="dynamic" onClick="document.value_list.form_var[<?=$k ?>][number].value='';document.value_list.form_var[<?=$k ?>][number].disabled=true"<?=($v['type']=='dynamic'?' selected':''?>>Dynamic</option>
                </select>
            </td>
        </tr>
        <?php
    }
    ?>
    </form>
    How would I get the JavaScript embedded in the options of <select name="form_var[<?=$k ?>][type]"> to function correctly, disabling or enabling the specific form_var[<?=$k ?>][number] field? The above does not seem to work as-is.

    I figure this has probably been asked before but I couldn't find a correct solution to what I was looking for...unless I have misread some other posts. I am not so great with JavaScript, unfortunately.

    Thanks in advance for any help!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    It does not work because <option> tags don't have onclick event. You should use onchange event in <select> tag.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I think this is what you want.
    Code:
    <head>
    <script type="text/javascript">
    function enableDisableField(oSel, index){
      var oFld = oSel.form.elements["form_var[" + index + "][number]"];
      if (oSel.selectedIndex == 0){ //Static
         oFld.disabled = false;
      }
      else { //Dynamic
        oFld.value = "";
        oFld.disabled = true;
      }
    }
    </script>
    </head>
    <body>
    <form name="value_list" method="POST" action="value_lister.php">
    <?php
    foreach ($somevar as $k=>$v) {
        ?>
        <tr>
            <td>
                <input name="form_var[<?=$k ?>][name]" type="text" size="40" value="<?=$v['name'] ?>" />
            </td>
            <td>
                <input name="form_var[<?=$k ?>][number]" type="text" size="8" value="<?=$v['number'] ?>" />
            </td>
            <td>
                <select name="form_var[<?=$k ?>][type]" onchange="enableDisableField(this, <?=$k ?>)">
                    <option value="static"<?=($v['type']=='static'?' selected':'') ?>>Static</option>
                    <option value="dynamic"<?=($v['type']=='dynamic'?' selected':'') ?>>Dynamic</option>
                </select>
            </td>
        </tr>
        <?php
    }
    ?>
    </form>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks! That looks like what I need. I will try it and see if it works for me.

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks, this worked for me. Took some time before I had chance to go back and try this again.


  •  

    Posting Permissions

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