1

The below is a snippet

<tr>
    <td>7</td>
    <td>Tick the Teaching Methods Used </td>
    <td>
        <input type="checkbox" id="lectures" name="lectures" value="lectures">Lectures &nbsp;
        <input type="checkbox" id="study" name="study" value="study">Case Study &nbsp;
        <input type="checkbox" id="audvid" name="audvid" value="audvid">Audio|Video &nbsp;
        <input type="checkbox" id="interactive" name="interactive" value="interactive">Interactive Methods &nbsp;
        <input type="checkbox" id="discussion" name="discussion" value="discussion">Discussion &nbsp;
        <input type="checkbox" id="role" name="role" value="role">Role Play &nbsp;
        <input type="checkbox" id="quiz" name="quiz" value="quiz">Quiz
    </td>
</tr>

and the validation code is

if ((document.form1.lectures.checked == false)
      && (document.form1.study.checked == false)
      && (document.form1.audvid.checked == false)
      && (document.form1.interactive.checked == false)
      && (document.form1.discussion.checked == false)
      && (document.form1.role.checked == false)
      && (document.form1.quiz.checked == false)) { 

    alert("Please check any one method"); 
    isValid = false;
}
return isValid;

How do i insert only the checked values into mysql database, implode doesn't seem to help

Edit : If i use the same "name" for all checkbox implode works but in that case I'm not able to validate

1

2 Answers 2

0

Use the same name for all your checkboxes. You said implode, so that's good. As for the validation, change it to use the IDs:

else if ((document.getElementById("lectures").checked == false)
      && (document.getElementById("study").checked == false)
      && (document.getElementById("audvid").checked == false)
      && (document.getElementById("interactive").checked == false)
      && (document.getElementById("discussion").checked == false)
      && (document.getElementById("role").checked == false)
      && (document.getElementById("quiz").checked == false)) { 

    alert("Please check any one method"); 
    isValid = false;
}
return isValid;
Sign up to request clarification or add additional context in comments.

Comments

0

Checkboxes are only put in POST, if selected (checked).

PHP:

$keys = explode(',','lectures,study,audvid'); //get all keys
$result = array();

foreach ($keys as $key) {
  if (isset($_POST[$key]) && $_POST[$key]) { //selected!
    $result[] = $key;
  }
}

print_r($result); //only selected checkeboxes are in result.

Comments

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.