Forum Moderators: coopster
i'm having a problem using checkbox selection
i pull from a table all the names in my db and have a checkbox in front of each name
i want to assign those names checked to a certain job which i have selected from a dropdown list
here is my code to select names on the form
echo "Select the person from checkbox to add to the job<br>";
while ($rowc = mysql_fetch_array($resultc))
{
extract($rowc);
echo "<input type = 'checkbox' name = 'folks[$icontact_id]'
value = '{$rowc['icontact_id']}' > {$rowc['flname']}, {$rowc['ictitle']} ";
echo "<br>";
}
echo "</select>\n";
here is my code on anothe php post page
$icontact_id = $_POST['folks']['icontact_id'];
$event_id = $_POST['event_id'];
$query="INSERT INTO jobs (job_id, icontact_id, event_id)
VALUES ('null','".$icontact_id."','".$event_id."')";
when i do this it only post the event_id to the jobs table and not even 1 name from the names selected by checkbox on form page
i may be wrong but i think i need to make some sort of an array of each name and event id them input all as a loop or something
any suggestions
thanks
jd
is it possible to have next to each individuals name a dropdown box where you can also select the persons jobtitle which feeds from the jobtitles table
on submit you post the person selected with the checkbox along with the job title selected from the dropdown box
the form looks like this
[checkbox] joe smith (dropdown select jobtitle)
[checkbox] mary smith (dropdown select jobtitle)
[checkbox] fran jones (dropdown select jobtitle)
the checkbox submits 2 values, the individual_id and the jobtitle_id
i've been trying for 2 days to figure this out
any suggestions
thanks
jd
is telling php (at post time) that you have an array of checkboxes named 'folks'. When a form is posted with checkboxes, only the checkboxes that are actually checked will appear in the array. So if you have on your form:
folks[1]
folks[2]
folks[8]
folks[9]
and the only one that the user checks is folks[8], then your checkbox array will have one item:
$_POST['folks'][8]
On your post page, you've got this:
$icontact_id = $_POST['folks']['icontact_id'];
which doesn't work because there is no array element with the literal text "icontact_id". To get to the checked items on your post page, you have to look for them one by one:
foreach($_POST['folks'] as $icontact_id => $value) {
//stuff
}
Now, inside the loop, $icontact_id will be the same as the one you assigned in your form and $value will be the checkbox's value that you assigned, which is also icontact_id.
For your second post, the checkbox itself can't submit two values. You could make another array for the dropdown that coincides with the contact_id (this is back to where you echo the checkbox when you're building the form):
echo "<select name=\"title[$icontact_id]\">";
Unlike the checkboxes, all of the title selects will be present in the posted array - that is, even though only folks[8] shows up, $_POST['title'][1], $_POST['title'][2], $_POST['title'][8], and $_POST['title'][9] will all have values for you. This isn't a problem, you just need to be aware of it so that you only store the value for checkboxes that have been checked. You could do that inside the foreach loop.
should i change
echo "<input type = 'checkbox' name = 'folks[$icontact_id]'
to
echo "<input type = 'checkbox' name = 'folks[]'
and on post page
change
$icontact_id = $_POST['folks']['icontact_id'];
to
something else or leave as is and do the foreach
foreach($_POST['folks'] as $icontact_id => $value)
{
//stuff
}
where to i use the $vaule, in part of my input statement
how do i get my inset statement to keep inserting until all my checkboxes are inserted
and
will each insert contain the event_id which i had selected at the very begining of the form with a dropdown box
this is about the most complicated thing i've done so far, once i get this to work then i'll think about a way to add job title to each of these individuals, in one form instead of 1 at a time like i do it now
thanks for your help
jd
and keep the contact_id in the value; you don't really need it both places.
Then the foreach changes to
foreach($_POST['folks'] as $icontact_id) {
Just as a side note and hopefully it won't confuse you, but you could make the same change to the foreach without changing the checkbox echo; I did it that way to point out to you that you've got the same information in two places.
If your event id is coming back to you from the post, inside the foreach it's still available, so you could do:
$event_id = intval($_POST['event_id']); // if it's numeric. You want to do this to 'clean' the value; never trust any data that comes to you from outside.
foreach($_POST['folks'] as $icontact_id) {
$icontact_id = intval($icontact_id);
mysql_query("INSERT INTO table (event_id,contact_id) VALUES ($event_id,$icontact_id)");
}
Having the insert inside the foreach will cause it to insert until all the checked boxes have been done.
$event_id = $_POST['event_id'];
foreach($_POST['folks'] as $icontact_id)
{
$icontact_id = intval($icontact_id);
$query="INSERT INTO jobs (job_id, icontact_id, event_id)
VALUES ('null','".$icontact_id."','".$event_id."')";
}
where do i put
$event_id = intval($_POST['event_id']);
and how can i echo what was input
i usually do this 1 record at a time, and doing it many at a time has me confused
That needs to go up inside the foreach as well.
To echo, put that inside the foreach, too.
$event_id = $_POST['event_id'];
foreach($_POST['folks'] as $icontact_id)
{
$icontact_id = intval($icontact_id);
$query="INSERT INTO jobs (job_id, icontact_id, event_id)
VALUES ('null','".$icontact_id."','".$event_id."')";
echo "<p>$query</p>\n";
mysql_query($query);
}
Where you have the event_id line in the post processing is fine - this assumes that you do have, somewhere on the form, an <input type="textbox" name="event_id"> or <input type="hidden" name="event_id" value="$event_id_that_came_from_somewhere">, right?
i"m getting closer to what i want
the last checkbox individual post twice
here is my code
foreach($_POST['folks'] as $icontact_id)
{
$icontact_id = intval($icontact_id);
$query="INSERT INTO jobs (job_id, icontact_id, event_id)
VALUES ('null','".$icontact_id."','".$event_id."')";
echo "<p>$query</p>\n";
mysql_query($query);
}