1

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

2 Answers 2

1

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.

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

1 Comment

Thanks for a quick and prompt resolution !! @emustac This exactly what I wanted !! :)
1

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>

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.