Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. simple javascrpt: checkbox

simple javascrpt: checkbox

Scheduled Pinned Locked Moved JavaScript
databasequestionjavascriptphphtml
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndyInUK
    wrote on last edited by
    #1

    Hi Devs,

                <label class="label" for="ex11">Label:</label>   
                <input type="checkbox" id="ex11" /> 
    

    and javascript

             $("#ex11")
    		// attach the iButton behavior
    		.iButton({
    		 /\*  labelOn: "Yes"
    		 , labelOff: "No"
    		 ,\*/ change: function ($input){
    				// update the text based on the status of the checkbox
    				$("#status").html($input.is(":checked") ? "Switched ON" : "Switched OFF");
    				
    			}
    		})
    		// trigger the change event (to update the text)
    		.trigger("change");
    });
    

    Now the problem is - When check box is checked - i want to pass a variable with 1 value that will be stored in the database. How can i do that in current situation?? Do i have to pass the variable from javascript to php and then sore it in database ? If yes then what's the best way to pass variable with 1 value to php ?? Thanks

    E 1 Reply Last reply
    0
    • A AndyInUK

      Hi Devs,

                  <label class="label" for="ex11">Label:</label>   
                  <input type="checkbox" id="ex11" /> 
      

      and javascript

               $("#ex11")
      		// attach the iButton behavior
      		.iButton({
      		 /\*  labelOn: "Yes"
      		 , labelOff: "No"
      		 ,\*/ change: function ($input){
      				// update the text based on the status of the checkbox
      				$("#status").html($input.is(":checked") ? "Switched ON" : "Switched OFF");
      				
      			}
      		})
      		// trigger the change event (to update the text)
      		.trigger("change");
      });
      

      Now the problem is - When check box is checked - i want to pass a variable with 1 value that will be stored in the database. How can i do that in current situation?? Do i have to pass the variable from javascript to php and then sore it in database ? If yes then what's the best way to pass variable with 1 value to php ?? Thanks

      E Offline
      E Offline
      Evan Gallup
      wrote on last edited by
      #2

      The way I would do so (if you want to do it on the fly, without the user changing pages) is to use AJAX. It's really quite simple actually, don't let AJAX scare you off if you haven't ventured into it yet.

      function saveCheckBoxValue(value)
      {
      var xmlhttp;
      if (window.XMLHttpRequest){
      xmlhttp = new XMLHttpRequest();
      }
      else{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }

      xmlhttp.onreadystatechange=function(){
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
      [DO SOMETHING HERE SUCH AS THE EXAMPLE BELOW THIS]
      document.getElementById('#ex11').innerHTML += "Value saved!";
      }
      }

      xmlhttp.open("GET","[LOCATION TO PHP SCRIPT HERE]?value=" + value,true);
      xmlhttp.send();
      }

      Then, in the HTML code for your checkbox, all you must do is this:

      <input type="checkbox" id="ex11" value="" onchange="saveCheckBoxValue(this.value)" />

      Not sure if this is what you were looking for, but I figured I'd give my two cents. The best part about this is that you can create multiple checkboxes, but still only use that one js function to save the values. You will most likely need to tweak this to your needs, such as the value that the checkbox must have, but it's a start at least. Happy coding! :-D

      A 1 Reply Last reply
      0
      • E Evan Gallup

        The way I would do so (if you want to do it on the fly, without the user changing pages) is to use AJAX. It's really quite simple actually, don't let AJAX scare you off if you haven't ventured into it yet.

        function saveCheckBoxValue(value)
        {
        var xmlhttp;
        if (window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
        }
        else{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
        [DO SOMETHING HERE SUCH AS THE EXAMPLE BELOW THIS]
        document.getElementById('#ex11').innerHTML += "Value saved!";
        }
        }

        xmlhttp.open("GET","[LOCATION TO PHP SCRIPT HERE]?value=" + value,true);
        xmlhttp.send();
        }

        Then, in the HTML code for your checkbox, all you must do is this:

        <input type="checkbox" id="ex11" value="" onchange="saveCheckBoxValue(this.value)" />

        Not sure if this is what you were looking for, but I figured I'd give my two cents. The best part about this is that you can create multiple checkboxes, but still only use that one js function to save the values. You will most likely need to tweak this to your needs, such as the value that the checkbox must have, but it's a start at least. Happy coding! :-D

        A Offline
        A Offline
        AndyInUK
        wrote on last edited by
        #3

        Hi Evan, Thanks for your reply. This make sense and works but it doesn't seem to work when i check/uncheck more than once. I would like the checkbox with two values. When checked -> 1 should be stored in the database and when unchecked -> 0 should be stored in db. I can do something like this but then it will display two checkbox and that doesn't really help.

        <input name="users" type="checkbox" value="1" onchange="saveCheckBoxValue(this.value)" />
        <input name="users" type="checkbox" value="0" onchange="saveCheckBoxValue(this.value)" />

        I guess am missing something very basic?? Thanks Andyyy

        E 1 Reply Last reply
        0
        • A AndyInUK

          Hi Evan, Thanks for your reply. This make sense and works but it doesn't seem to work when i check/uncheck more than once. I would like the checkbox with two values. When checked -> 1 should be stored in the database and when unchecked -> 0 should be stored in db. I can do something like this but then it will display two checkbox and that doesn't really help.

          <input name="users" type="checkbox" value="1" onchange="saveCheckBoxValue(this.value)" />
          <input name="users" type="checkbox" value="0" onchange="saveCheckBoxValue(this.value)" />

          I guess am missing something very basic?? Thanks Andyyy

          E Offline
          E Offline
          Evan Gallup
          wrote on last edited by
          #4

          Yes, something very very very basic. You need to look at the AJAX script.

          function saveCheckBoxValue(value)
          {
          var xmlhttp;
          if (window.XMLHttpRequest){
          xmlhttp = new XMLHttpRequest();
          }
          else{
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }

          xmlhttp.onreadystatechange=function(){
          if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
          if(document.getElementById('users').checked) document.getElementById('users').value = '1';
          else document.getElementById('users').value = '0';

          document.getElementById('#ex11').innerHTML += "Value saved!";
          }
          }

          xmlhttp.open("GET","[LOCATION TO PHP SCRIPT HERE]?value=" + value,true);
          xmlhttp.send();
          }

          Then just make sure you set the 'id' attribute on your checkbox.

          <input name="users" id="users" type="checkbox" value="0" onchange="saveCheckBoxValue(this.value)" />

          Keep in mind what the value should start as based on the value from the database if needed. That's opening a whole new can of worms. You're going to need to get the current value from the database with PHP, and then use and if-else like the one in the javascript above to set the initial value. There are a lot of things going on in your script, you really have to keep your mind open and figure out the logic behind everything that's happening to be a successful programmer. Good luck! -Evan

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups