Siddharth Pujara
Siddharth Pujara

Reputation: 135

Using checkbox as input for each record in a mysql table

I am trying to use a check box as input in form where the records are printed from a mysql table. Following is the code which I have used :-

<body>
<form name='checkbox_text' method="post" action="submit_checkbox.php">
<select name="candidate_name" id="option2">     

<b><h3>Select the candidates :-</h3></b> 
<label class="q" for="qq1" </label>
<input type="hidden" name="qq1[]" value="null">
<?php
    mysql_connect("localhost", "root", "root1") or die (mysql_error ());

    mysql_select_db("my_database") or die(mysql_error());

    $strSQL = "SELECT Name FROM Candidate ORDER BY Name";

    $rs = mysql_query($strSQL);
    while($row = mysql_fetch_array($rs)) 
    {
    $man = $row['Name'];
    Print ("<option>"); 
    echo $man;
    Print ("</option>");
    }
    mysql_close();
    ?>
</select>
</form>
</body>

I am able to print all the table contents but I need a checkbox in front of each name, user shall select few names and pass those values in submit_checkbox.php

Upvotes: 1

Views: 203

Answers (2)

emustac
emustac

Reputation: 425

maybe something like this?

while($row = mysql_fetch_array($rs)) {
    $man = $row['Name'];
    echo '<input type="checkbox" value="'.$man.'" name="checkbox[]" />'; 
    echo $man;
}

if you want multiple values after you submit form, you have to use [] in name attribute. you'll get an array with all selected names after you post the form.

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31739

Try with Multiple select

<h3>Select the candidates :-</h3>
<select name="candidate_name[]" id="option2" multiple>     
<?php
    mysql_connect("localhost", "root", "root1") or die (mysql_error ());

    mysql_select_db("my_database") or die(mysql_error());

    $strSQL = "SELECT Name FROM Candidate ORDER BY Name";

    $rs = mysql_query($strSQL);
    while($row = mysql_fetch_array($rs)) 
    {
    $man = $row['Name'];
    Print ("<option>"); 
    echo $man;
    Print ("</option>");
    }
    mysql_close();
    ?>
</select>

Upvotes: 1

Related Questions