0

Dear experties,
I have a problem to insert checkbox value that called from database. How can I identify each checkbox's value?
This is my code to show the checkbox. Dont know how to post the value to insert into database if checked.
Please give me idea to solve it. Thank you.

<?php
$sql="SELECT * FROM dept_receiver";
$result=mysql_query($sql);


// run through the results from the database, generating the checkboxes 
while ($row = mysql_fetch_assoc($result)) { 
   echo "\n\t"; 
   echo "<input id='{$row['rcvr_id']}' type='checkbox' name='rcvr[]' value='{$row['rcvr_id']}'>{$row['rcvr_name']}"; 

} 


?>
2
Contributors
6
Replies
8
Views
4 Years
Discussion Span
Last Post by HasNor
0

Problem should lie with your ' symbol.
Maybe you can do one step at a time. Easier for debugging as well.

while ($row = mysql_fetch_assoc($result)) { 
   $value = $row['rcvr_id']; //you can check the output if it doesn't work
   echo "<input id='$value' type='checkbox' name='rcvr[]' value='$value'>$value<br>"; 
} 
0

Thanks Javvy for reply.
So how can I post each value to Insert the value into database in 1 field? If selected more than 1, it should be saperate with ";"..
I dont know how to post the value..the value display is correct regarding to database.
please help me..

0

Since you already put them in an array, the next page when the form data are POSTed to will receive the array in $_POST['rcvr'];

To store them in 1 field concat with ';', you can use the implode() function.
$result = implode(";", $array);

Read more about implode from http://php.net/manual/en/function.implode.php

Edited by Javvy

0

Javvy, i tried use this code but no value insert into database. Here my code.

add_form.php

<?php
$sql="SELECT * FROM dept_receiver";
$result=mysql_query($sql);


// run through the results from the database, generating the checkboxes 
while ($row = mysql_fetch_assoc($result)) { 
   echo "\n\t"; 
   $value = $row['rcvr_id']; //you can check the output if it doesn't work3.   
   echo "<input id='rcvr' type='checkbox' name='rcvr' value='{$row['rcvr_id']}'>{$row['rcvr_name']}"; 

} 


?>

db_add.php

<?php
extract ($_POST);
include 'connection/db_connect.php';

if(isset($_POST['Save']))

if(isset($rcvr))
 $dept_rc=$rcvr."; ";
 else
 $dept_rc=NULL;

$sql = 'INSERT INTO list SET

        receiver="'.$_POST['dept_rc'].'"';

$result=mysql_query($sql);

?>
0

add_form.php

<?php
$sql="SELECT * FROM dept_receiver";
$result=mysql_query($sql);
// run through the results from the database, generating the checkboxes 
while ($row = mysql_fetch_assoc($result)) { 
   $value = $row['rcvr_id']; //you can check the output if it doesn't work
   echo "<input id='$value' type='checkbox' name='rcvr[]' value='$value'>$value<br>"; 
} 
?>

db_add.php

<?php
include 'connection/db_connect.php';

if ($_POST) {
    $rcvr = $_POST['rcvr'];

    $value = implode(";", $rcvr);

    $sql = "INSERT INTO list SET receiver = '$value';
    $result = mysql_query($sql);
}
?>

Note: Not tested. Test the output to debug

Edited by Javvy

0

wow!! thanks a lot Javvy!!!
i got it as per i wished!!
awesome!! thank you so much.. (^_^)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.