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 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post

    Problem With Simple AJAX Function

    I have a checkbox that when checked, brings up a little form for someone to fill out. That works great, but when it's unchecked, the alternate condition of the form (display nothing) does not display.

    Here is the checkbox:

    Code:
    <input type="checkbox" name="mailing_list" value="yes" onClick="mailingList(this.value)" />&nbsp;&nbsp;Check box to be on our mailing list
    And then the PHP script that is subsequently triggered is basically this:

    PHP Code:
    if($answer == "yes") {
     echo 
    'This displays perfectly when the checkbox is checked';
    } else {
     echo 
    'This never displays when the checkbox is unechecked';

    I'm sure the mistake I've made is obvious but I can't figure it out.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    The value of a checkbox DOES NOT CHANGE, no matter if it is checked or unchecked.

    So when you call onClick="mailingList(this.value)" you are *REALLY* ALWAYS doing onclick="mailingList('yes').

    Personally, I would rewrite the code to do onclick="mailingList(this.checked) but that would mean changing your PHP code, too. So a simple solution:
    Code:
    onclick="mailingList(this.checked ? 'yes' : 'no')"
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    cgdtalent (07-12-2013)

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    The value of a checkbox DOES NOT CHANGE, no matter if it is checked or unchecked.

    So when you call onClick="mailingList(this.value)" you are *REALLY* ALWAYS doing onclick="mailingList('yes').

    Personally, I would rewrite the code to do onclick="mailingList(this.checked) but that would mean changing your PHP code, too. So a simple solution:
    Code:
    onclick="mailingList(this.checked ? 'yes' : 'no')"
    Thanks a lot! That worked great!!


  •  

    Posting Permissions

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