SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checkbox onclick submit this checkbox only?

    Hi all,

    New to JavaScript, but quite familiar with PHP. Am trying to get my head around this one. I have a dozen or so checkboxes in a form by themselves. I want whenever someone clicks a checkbox to automatically submit the form, but only to recognize the box that was checked.

    Basically, I am trying to avoid cycling through the boxes in PHP and updating 12 rows when only one checkbox was clicked.

    Any ideas?

    Currently, I'm here: onclick="this.form.submit();"

    LOL. Not very far.

    Thx!
    JP

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
          onClick="document.form.submit();"

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vijiruba, your code doesn't really work.

    jpfour23. Once a checkbox is checked it submits the form right? The checkbox's value is then submitted, what is the exact problem you are having? Is it not submitting?

  4. #4
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    <body>
    
    <form action="index.php">
    <input type="checkbox" name="chk" onclick="if(this.checked){this.form.submit()}">
    <input type="checkbox" name="chk" onclick="if(this.checked){this.form.submit()}">
    <input type="checkbox" name="chk" onclick="if(this.checked){this.form.submit()}">
    <input type="checkbox" name="chk" onclick="if(this.checked){this.form.submit()}">
    </script>

  5. #5
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I should have clarified better. muazzez is on the right track. The problem then is if the box is already checked and I uncheck it.

    Any ideas?

    Thx,
    JP

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you want to submit when the user unchecks it? You need to clarify this better...

  7. #7
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I have 12+ checkboxes that are listed from a PHP array. Some will be checked, others may not be checked. When someone checks (or unchecks) a box, I want the form to submit, but only to submit the change that was made (essentially, the box that was checked).

    In PHP language I would logically think...

    IF Checkbox1 was changed, then update the database with this new value.

    Make sense?

    Thx,
    JP

  8. #8
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like you want some AJAX which can send information about the one change to the server instead of submitting the whole form, like the posted code examples show.

    Before you use AJAX though, you should read up on JavaScript Best Practices.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •