Jump to content

Working with Dynamic form checkboxes...


pthurmond

Recommended Posts

Hello everyone, first of all thanks for taking the time to look at this for me and especially if you reply.
I am a newbie with PHP that has alot of C++ experience and I am trying to develop a participant signin system for a local non-profit organization. This website I am developing will help them generate reports on participant activities that will help them apply for more grants to keep it running. It is a youth center organization in Kansas City.

Anyways here is my problem...
Every day that the participants come to these locations is considered an "Event" in my program. Each event will have a limited list of activities that are going on at each event and at each location. Each event is saved in the MySQL database and contains one field that holds a list of activity ID numbers that are separated by commas (example: 4,7,21,33,40 etc.). Now these activities (there name, ID, description, and image link) are stored in another table in the db. This list of activities is going to be constantly changing so I cannot do a hard coded check for each activity under each event. The check itself has to be dynamic. I am already aware of and am using the explode function to divide the IDs into an array of Activity IDs to check each one. Now my problem is primarily concerned with processing the list of selected activity IDs. When each participant tries to sign in for the day he/she does a search by their name, then once their name is selected they are sent to another page that dynamically displays a list of all possible activities for that days event. These are all checkboxes, so figuring this next part out becomes kind of tricky. I am confident in my solution for displaying the list of checkboxes dynamically with the following code:

[code]
if($result)
{
$row = mysql_fetch_array($result, MYSQL_NUM);
$astring = $row[0]; //Reads in the string of activities
}
else
{
$message .= '<p>Your search could not be completed due to a system error.</p><p>' . mysql_error() . '</p>';
}

//Code to seperate the activity ID numbers from the event string that is seperated by commas, stores them in an array
$actarray = explode(',', $astring);
$aCount = count($actarray);


//Get the name, picture path, and description of each activity
//Current query status

//Use a loop to build each checkbox using the values in the array as a variable.
for($i = 0; $i < $acount; $i++)
{
$box = actarray[$i];

$query = "SELECT Name, Description, Picture FROM Activities WHERE Activity_ID = '$box' ";

//Run the query
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

if($result)
{
$row = mysql_fetch_array($result, MYSQL_NUM);
$name = $row[0]; //Activity name
$desc = $row[1]; //Activity description, this will be used for the alternative text
$pic = $row[2];  //Activity picture
}
else
{
$message .= '<p>Your search could not be completed due to a system error.</p><p>' . mysql_error() . '</p>';
}

echo '<tr>';
echo '<td width="25"><input name="';
echo $box;
echo '" type="checkbox" value="5"></td>';
echo '&nbsp;</tr>';
echo '<td><img src="';
echo $pic;
echo '" border="1" alt="';
echo $desc;
echo '" valign="middle" align="center">';
echo '<tr>';
echo $name;
echo '</tr>';
echo '</td>';
echo '</tr>';
}
[/code]

I realize that I can use concatenation to cut down on the number of lines, but for readability issues I will leave it like this for now.
The concern I have is with the code on the next page used to process the code. Specifically checking whether or not the user checked the box (setting the value to true) on the previous page. I am accessing this info from the $_POST[''] array. I will show you my code below this but my question is can I use a variable in this array to identify the name of the checkbox to check? (example: $_POST['$box']). If I can do this then can I leave the variable like I did in my example (and the code below) or do I need to surround the variable with curly braces {}? Also is there an easier way to do this?

My code:
[code]
        if($result)
{
$row = mysql_fetch_array($result, MYSQL_NUM);
$astring = $row[0]; //Reads in the string of activities
}
else
{
$message .= '<p>Your search could not be completed due to a system error.</p><p>' . mysql_error() . '</p>';
}

//Code to seperate the activity ID numbers from the event string that is seperated by commas, stores them in an array
$actarray = explode(',', $astring);
$aCount = count($actarray);


//Use a loop to check each checkbox using the values in the array as a variable.
for($i = 0; $i < $acount; $i++)
{
$box = actarray[$i];

if (!(empty($_POST['$box'])))
{
if($activities)
{
$activities .= ',' . $_POST['$box'];
}
else
{
$activities = $_POST['$box'];
}
}
}
[/code]


My next question is if I create an array on one page, can I use it on another page once I go to that other page. To be more specific, will the array still exist with the same data for me to access once I move to another page?

Thank you all for taking the time to read this.


Sincerely,
Patrick Thurmond
Link to comment
Share on other sites

without reading all that - the easiet method of creating the array you want is to simply give all the checkboxes the same name and set the value to the id of the activity.

HTML will auto make an array if you use name="event[]" value="<?php echo $event_id; ?>" />.

the in the processing script you just need to loop through this array like so...

foreach($_POST['eventid'] as $key => $val)
{

}

beauty of checkboxes (and radio boxes) is that if they are not checked then they are not set...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.