• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Posting values from a checkbox

I created an online schedule for my department and all works fine. I now would like to add the posibility of making an event recursive. To accomplish this, I created a small section with textboxes which will contain the days of the month:
<input type="checkbox" name="weekly" value="0">Sunday
<input type="checkbox" name="weekly" value="1">Monday
etc.
How can I correctly post the values of the selected checkboxes? I'm using PHP. Thanks!
0
horalia
Asked:
horalia
  • 3
  • 2
1 Solution
 
nizsmoDeveloperCommented:
<input type="checkbox" name="weekly[]" value="0">Sunday
<input type="checkbox" name="weekly[]" value="1">Monday

By making the checkboxes name an Array, and then submitting the form through PHP then using foreach construct to output the array one by one with its values. Hope this is what you are looking for?

Let me know how you go.
0
 
gamebitsCommented:
First they should have different names and then you get the value like any other form input

$checkbox = $_POST['weekly'];

Keep i mind that the checkboxes who are not selected are not sent with the form data.
0
 
horaliaAuthor Commented:
nizsmo, I used that and when posting the values, I simply got Array. I think I need to have my foreach statement modified to read Arrays. Can you give me a hand with this? Here's the code that I have to post the values coming from the edit page:

foreach($_POST as $name=>$value)
{
  if (!empty($value))
        $$name = $value;
}
foreach($_POST as $key => $value)
{
  echo "$key: $value <br>";
}
die();

When I press the submit button, this is what I get for weekly:
weekly: Array

The rest of my values are being correctly posted.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
nizsmoDeveloperCommented:
Hmmm, how about something like this, it should work:

$myarray = $_REQUEST["weekly"];

foreach($myarray as $value)
{
echo $value;
// some more code here
}

This shoud work.

Let me know how you go.
0
 
horaliaAuthor Commented:
Exactly what I needed. Thanks!
0
 
nizsmoDeveloperCommented:
Glad to be of assistance :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now