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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Location
    India
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Selecting multiple check boxes....

    I have developed an application in PHP.The user will find the list of experts and this code is generated based on the experts data in database..

    It is working fine..

    The users selects the expert according to his wish ( checks using check box provided before the expert and there is no restriction in selecting experts) .

    Now he enters the question, he needs the reply and then he clicks on submit button.An email must be sent to the expert
    I need the javascript to find whether the USER CHECKED the Check boxes or NOT.

    Plz help ....

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    How are the checkboxes named? Do they have the same name or named differently with a pattern? Can you post a sample output for the checkboxes?

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Location
    India
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sure sir..

    This is my code...It is fetching data that is in mysql database tables,so i kept it in loop and it has only one name checkbox1....


    Code:
    mysql_connect("localhost","root") or die(mysql_error());
    mysql_select_db("db_soft") or die(mysql_error());
    
    $result = mysql_query("SELECT sfa_experts.display_name, sfa_skills.skill_name, sfa_skills.skill_desc
    FROM sfa_experts, sfa_skills, sfa_expert_skills
    WHERE sfa_experts.expert_uid = sfa_expert_skills.expert_uid
    AND sfa_skills.skill_uid = sfa_expert_skills.skill_uid");
    
    echo "<table border='1' align=center >
    <tr>
    <th>Expert Name</th>
    <th>Profile</th>
    <th>Area of Experience</th>
    </tr>";
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td> <input type='checkbox' name='checkbox1'>"  . $row['display_name'] . "</td>";
      echo "<td>" . $row['skill_name'] . "</td>";
      echo "<td>" . $row['skill_desc'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    mysql_close();

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    The checkboxes should have a value (e.g. expert_uid) so that the server will know which experts are checked when the form is submitted.
    Code:
    function validate(f){
      var chks = f.checkbox1;
      var checked = false;
      if (chks){ //checkbox(es) exist(s)
        if (chks.length){  //multiple checkboxes
          for (var i=0; i<chks.length; i++){
            if (chks[i].checked){
              checked = true;
              break;
            }
          }
        }
        else { //single checkbox
          checked = chks.checked;
        }
        if (!checked){
          alert("Please checked at least one expert.");
          return false;
        }
      }
      return true;
    }
    Code:
    <form name="frm" action="whatever.php" method="post" onsubmit="return validate(this);">
    
    ...checkboxes here
    
    <input type="submit" value="Submit" />
    </form>


  •  

    Posting Permissions

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