If checkbox.checked, post variables

Discussion in 'JavaScript' started by PoPSiCLe, Mar 27, 2009.

  1. #1
    Hey.

    I have a webform, where I would like to be able to post a couple of hidden form fields if one particular checkbox is checked.

    I tried something like this:
    
    <p><label>Adresser:</label><input id="address" type="checkbox" name="pm_street_address" /></p>
    
    <script type="text/javascript">
    if (document.forms.pm_street_address.checkbox.checked) {
    		<input type="hidden" name="pm_post_num" value="pm_post_num" />
    		<input type="hidden" name="pm_post_area" value="pm_post_area" />
    }
    </script>
    
    Code (markup):
    but that didn't work - at least nothing gets posted to the receiving page, it seems, whether the checkbox is checked or not (the "pm_street_adress" gets posted, of course).

    So, how would I approach this to get a couple of $_POST-variables from this, if the address-checkbox is checked?

    Anyone?
     
    PoPSiCLe, Mar 27, 2009 IP
  2. ergtron

    ergtron Peon

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You don't have a checkbox value. Assign a value to your checkbox such as value="yes".
    Then use something like this:

    if ($_POST['pm_street_address'] == 'yes') // If it's checked
    {
    //Get other variables

    }
     
    ergtron, Mar 27, 2009 IP
    PoPSiCLe likes this.
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Yeah, I figured out how to achieve my goal a little earlier :)

    You are absolutely right - I was trying to do it in an overcomplicated fashion :)
     
    PoPSiCLe, Mar 27, 2009 IP