0

I want to show my result from a checkbox with javascript I need it to send it to the database with php

I have column for each checkbox

if the chekbox is checked I take the variables and insert them into my database

else I Leave the column empty

the html code and javascript

var checks = document.getElementsByClassName('justtest');
          var str = '';
          for( i=0; i<3; i++){
              if(checks[i].checked === true){
                str += checks[i].value + " ";
              }

            }
alert(str);
<div class="form-group">
   <div class="col-md-3">
      <div class="checkbox">
         <label>
<input type="checkbox" value="test1" class="justtest" > Test1
         </label>
       </div>
      </div>
     </div>
    <div class="form-group">
    <div class="col-md-3">
     <div class="checkbox">
       <label>
 <input type="checkbox" value="test2" class="justtest"> test2
       </label>
      </div>
     </div>
    </div>
     <div class="form-group">
      <div class="col-md-3">
       <div class="checkbox">
         <label>
<input type="checkbox" value="test3" class="justtest"> test3
          </label>
         </div>
       </div>
      </div>

I try to do this but without any result

var checks = document.getElementsByClassName('justtest');
          var str = checks.length;
          for( i=0; i<3; i++){
              if(checks[i].checked === true){
                str[i]= checks[i].value;
              }

            }
alert(str[0]);
alert(str[1]);
alert(str[2]);

3 Answers 3

1

Few changes to your code, str is an array of result, and added a button which will execute the checking function, as for now the script was executed only once before you even had a change to select any option

var getValues = function () {
var str = [];
var checks = document.getElementsByClassName('justtest');
          for(var i=0; i<checks.length; i++){
              if(checks[i].checked){
                str.push( checks[i].value );
              } 
            }
  
  for( var x = 0; x < str.length; x++){
    alert (str[x]);
    }
  
  }
<div class="form-group">
   <div class="col-md-3">
      <div class="checkbox">
         <label>
<input type="checkbox" value="test1" class="justtest" > Test1
         </label>
       </div>
      </div>
     </div>
    <div class="form-group">
    <div class="col-md-3">
     <div class="checkbox">
       <label>
 <input type="checkbox" value="test2" class="justtest"> test2
       </label>
      </div>
     </div>
    </div>
     <div class="form-group">
      <div class="col-md-3">
       <div class="checkbox">
         <label>
<input type="checkbox" value="test3" class="justtest"> test3
          </label>
         </div>
        <button onclick="getValues()">Check</button>
       </div>
      </div>

Sign up to request clarification or add additional context in comments.

1 Comment

and if I want to get result to my php code I use name ?
1

it is possible to handle this from html point of view, where you give all the check box same input name (and use [] after it) , then let the post of the form take it to your php, and your php would see it as array, from javascript end you also get the values.

First of all let me show you the HTML version of it

each of your input field should look like this

  <input type="checkbox" name="justtest[]" value="owk 1">,
  <input type="checkbox" name="justtest[]" value="owk 2">

Note the justtest[] naming

when you do form post you can use ur php with any loop you like e.g

foreach ($_POST["justtest"] as $key => $value) {
 echo $value;
 }

While from javascript like you have done above Marcin C answer on this page still

if not satisfied here is another javascript approach which might help out too (Run the code snippet to see it in action)

function getInput(){
  var values = [];
  var inputs = document.getElementsByName("justtest[]");
  for (var i = 0; i <inputs.length; i++) {
    var inp=inputs[i];
    if(inp.checked){
     values.push(inp.value);
      }
  //alert("justtest["+i+"].value="+inp.value); this returns all with their value , we check for only checked ones
      }
 alert (values); //alert our values here
  }
<input type="checkbox" name="justtest[]" value="owk 1">
<input type="checkbox" name="justtest[]" value="owk 2">
<input type="checkbox" name="justtest[]" value="owk 3">
<input type="checkbox" name="justtest[]" value="owk 4">

<button onclick="getInput()">Get Input<button>

4 Comments

but in javascript how I can do it ?
if Marcin C answer did not satisfy your javascript , then are you trying to send this data to php after getting it using javascript , like as if it was a form post asynchronously
if it is , i will modify the answer and add the javascript part of it
run the code snippet to see the javascript implementation
0

Is you script tag in the head of your document? If so, this code will always fail as the document won't have initialised the checkboxes to be able to retrieve them.

Try moving the script to the bottom of the page (just above your end body tag)

1 Comment

the first script code is work, but I want to show each chekbox Alone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.