print <<<HERE
<form name="form1" method="post" action="">
<table width="100%" border="0">
<tr>
<td align="left" valign="top"><label>Topic
<input name="msg_subject" type="text" id="msg_subject" size="55" maxlength="35">
</label></td>
</tr>
<tr>
<td align="left" valign="top"><label>
<textarea name="msg_message" id="msg_message" cols="60" rows="5"></textarea> <input name="id" type="hidden" id="id" />
</label></td>
</tr>
<tr>
<td align="left" valign="top">
HERE;
?>
<?php
$s = " ";
$sql_list = mysql_query("SELECT * from bbcal_userlist ORDER BY firstname ASC");
$if_exist = mysql_num_rows($sql_list);
while($m_res = mysql_fetch_array($sql_list)){
?>
<label>
<input name="names" type="checkbox" value ="<?php echo $m_res . $s . $m_res; ?>" />
<?php echo $m_res . $s . $m_res; ?></label>
<br />
<?php
}
?>
<?php
print <<<HERE
</td>
</tr>
<tr>
<td align="left" valign="top"><label>
<input type="submit" name="submit" id="submit" value="Create a topic">
</label></td>
</tr>
</table>
</form>
HERE;


So I have a checkboxes running from database, And lets see i have:

$_POST
$_POST
$_POST

f (isset($_POST)) {$msg_message = $_POST;}
if (isset($_POST)) {$msg_subject = $_POST;}
if (isset($_POST)) {$namest = $_POST;}


So I need to do something like: FOREACH $names
{
mysql_query("INSERT INTO tablename (subject, name) VALUES ($msg_subject, $names)");
}


So basicaly there are going to be insert into two different tables. 1st table will contain the subject, message, id. The second table should contain the same id as the first table, and new row for each subsribtion.

EXAMPLE

1st table

ID: "5761" SUBJ: "Hello world" MSG: "here is a message"

2 nd table

S_ID: "5761" NAME: "username1"
S_ID: "5761" NAME: "username2"
S_ID: "5761" NAME: "username3"
S_ID: "5761" NAME: "username4"
S_ID: "5761" NAME: "username5"

If you can see each record for the second table should have the same ID and new row for each checked checkbox.
The first table is running okay, I just need to insert a new row for the second table.... Reply if you can do that... or if there is another way of doing that...


I just saw a similar topic about it, but I am pretty new to php. Any help would be appreciated

Recommended Answers

All 4 Replies

According to this page (http://us3.php.net/manual/en/language.variables.external.php):
---
When you are using checkboxes to submit multiple choices, there is no need to use the complex method where you assign a unique name to each checkbox.

Instead, just name each checkbox as the same array, e.g.:

<input type="checkbox" name="names[]" value="foo" />
<input type="checkbox" name="names[]" value="bar" />
<input type="checkbox" name="names[]" value="baz" />

This way your $_POST["names"] variable will return as an array containing all and only the checkboxes that were clicked on.

MY QUESTION IS WHAT IS THE SYNTAX FOR DOING THAT? WOULD YOU PLEASE HELP ME WITH THE "FOREACH" COMMAND? PLEASE! NOT DOING FOR MYSELF, BUT FOR CHURCH AUDIO LIBRARY...

According to this page (http://us3.php.net/manual/en/language.variables.external.php):
---
When you are using checkboxes to submit multiple choices, there is no need to use the complex method where you assign a unique name to each checkbox.

Instead, just name each checkbox as the same array, e.g.:

<input type="checkbox" name="names[]" value="foo" />
<input type="checkbox" name="names[]" value="bar" />
<input type="checkbox" name="names[]" value="baz" />

This way your $_POST["names"] variable will return as an array containing all and only the checkboxes that were clicked on.

MY QUESTION IS WHAT IS THE SYNTAX FOR DOING THAT? WOULD YOU PLEASE HELP ME WITH THE "FOREACH" COMMAND? PLEASE! NOT DOING FOR MYSELF, BUT FOR CHURCH AUDIO LIBRARY...

here is how you would do a for each

foreach($_POST["names"] as $strValue)
{
    //$strValue will contain the value of the selected checkboxes
}

here is how you would do a for each

foreach($_POST["names"] as $strValue)
{
    //$strValue will contain the value of the selected checkboxes
}

OMG, awesome. Thank you so much. I was looking for that for almost 4 days. Thank you so much, again...

you could also use this:

foreach($_POST["names"] as $index => $strValue)
{
    //$strValue will contain the value of the selected checkboxes
}

if you wish to keep track of your index... ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.