Welcome to WebmasterWorld Guest from 54.196.175.173

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Form Check Boxes

     

captainron19

4:53 pm on Jan 27, 2009 (gmt 0)

5+ Year Member



I am using a form that I will need multiple checkboxes.

My intention is to have the days of the month listed for people to submit their availability to work

1st
2nd
3rd
etc etc

Each day will have two check boxes: Days and Nights

Every time I try to input these fields into the formmail I am using the email showing the data just shows: "Array"

Can anyone help me with what I am doing wrong?

her eis an example of the checkboxes for the first day of the month

1st: Days: <input type="checkbox" name="1st[]" value="Days"><br>
Nights: <input type="checkbox" name="1st[]" value="Nights"><br>

penders

5:28 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



The value(s) of these fields are returned as an array (an array within your $_GET[] or $_POST[] array - whichever method you are using to post your form data) and consequently you need to step through this array to access the values. If you see just "Array" then you aren't stepping through this array.

Something like:

foreach ($_POST['first'] as $value) { 
echo 'Checkbox Set: '.$value.'<br>';
}

Btw, the value of the NAME attribute should begin with a letter, not a number.

captainron19

5:34 pm on Jan 27, 2009 (gmt 0)

5+ Year Member



So do I have to put that code in for every day of the month that I want someone to submit availability for? If so, where do I place it? In the PHP file or in the html file that has the form?

paolo sweden

4:50 pm on Jan 28, 2009 (gmt 0)

5+ Year Member



I don't know if I got your point correctly, however it seems to me that you need something like this:

A] In the page where you have the form:

<form method="post" action="action_file.php">
<?php
for($i=1; $i<=30; $i++)
{
echo 'Day '.$i.' <input type="checkbox" name="day_'.$i.'[]" value="Days"><br />';
echo 'Night '.$i.' <input type="checkbox" name="day_'.$i.'[]" value="Nights"><br /><br />';
}
?>
<input type="submit" name="submit" value="Go" />
</form>

B] In the page the receive the form inputs:

if(isset($_POST['submit']) && $_POST['submit']=="Go")
{
for($i=1; $i<=30; $i++)
{
if(isset($_POST['day_'.$i]))
{
echo "<b>You chose day".$i.":</b> ";
foreach($_POST['day_'.$i] AS $value)
{
echo "Value: ".$value."<br />";
}
}
}
}

Hope this might help you!

Paolo

captainron19

5:36 pm on Jan 28, 2009 (gmt 0)

5+ Year Member



A little confused...

When i placed the text you had (to go in the form) it is showing the coding on the web page. (Rather than the check boxes)

What do i do with the second part? Put it somewhere int he action form?

captainron19

5:41 pm on Jan 28, 2009 (gmt 0)

5+ Year Member



Disregard the last post - I realized I had the rename the form with a php extension but still having a problem.... when i submit it and receive the email (I put in availability for days and nights on the 1st and 3rd) here is the data that got sent...

day_1: Array

day_3: Array

paolo sweden

6:09 pm on Jan 28, 2009 (gmt 0)

5+ Year Member



1] Open a new php file and copy this code:

<?php
/*AFTER SUBMITTING THE FORM*/
if(isset($_POST['submit']) && $_POST['submit']=="Go")
{
for($i=1; $i<=30; $i++)
{
if(isset($_POST['day_'.$i]))
{
echo "<b>You chose day".$i.":</b> ";
foreach($_POST['day_'.$i] AS $value)
{
echo "Value: ".$value."<br />";
}
}
}
die();
}
?>

<!-- ACTUAL FORM-->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
for($i=1; $i<=30; $i++)
{
echo 'Day '.$i.' <input type="checkbox" name="day_'.$i.'[]" value="Days"><br />';
echo 'Night '.$i.' <input type="checkbox" name="day_'.$i.'[]" value="Nights"><br /><br />';
}
?>
<input type="submit" name="submit" value="Go" />
</form>

2] Launch it and check, for instance, night for the 28th, day for the 29th, and both day and night for the 30th. Submit the form; you should get this:

You chose day28: Value: Nights
You chose day29: Value: Days
You chose day30: Value: Days
Value: Nights

If you can see this...it's good starting point! In my opinion your mistake is that you don't step through the array, like I do when I write

foreach($_POST['day_'.$i] AS $value)
{
echo "Value: ".$value."<br />";
}

Paolo

captainron19

6:31 pm on Jan 28, 2009 (gmt 0)

5+ Year Member



You have me completely lost now

Sorry but I am no good in PHP

I created a new form based on the first set of coding you have and named it formmail_test.php

but what do I do with the rest of the code? The second set you have doesnt show anywhere to point to the formmail

paolo sweden

6:33 am on Jan 29, 2009 (gmt 0)

5+ Year Member



Don't worry! Try to do like this:

-Open a new php file with your favourite text-editor and copy the entire code I posted.
-Save such a file (i.e. my-form.php) in your local server directory (which should be /htdocs if you're using apache)
-Launch it in your browser (http://localhost/my-form.php)
-Check randomly some values and then submit the form by clicking on the button at the very bottom of the page
-See what you get...

What I was trying to tell you in my previous post is that if you have a variable (i.e. $my_var) which is an array, if you say echo $my_var you'll see "Array", which is not exactly what you're aiming to do. To be able to see the values stored in the array you have to step through it by using, for instance, the "foreach" statement.

So, if you have
$my_var=array('key1'=>'value1', 'key2'=>'value2');
if you just echo $my_var you'll see "Array" on your screen but if you:
foreach ($my_var AS $val)
{
echo $val."<br />";
}
you'll get:

value1
value2

that's the actual values stored in the array.

Hope to have been more exhaustive than before ;-)

Paolo

paolo sweden

6:34 am on Jan 29, 2009 (gmt 0)

5+ Year Member



Don't worry! Try to do like this:

-Open a new php file with your favourite text-editor and copy the entire code I posted.
-Save such a file (i.e. my-form.php) in your local server directory (which should be /htdocs if you're using apache)
-Launch it in your browser (http://localhost/my-form.php)
-Check randomly some values and then submit the form by clicking on the button at the very bottom of the page
-See what you get...

What I was trying to tell you in my previous post is that if you have a variable (i.e. $my_var) which is an array, if you say echo $my_var you'll see "Array", which is not exactly what you're aiming to do. To be able to see the values stored in the array you have to step through it by using, for instance, the "foreach" statement.

So, if you have
$my_var=array('key1'=>'value1', 'key2'=>'value2');
if you just echo $my_var you'll see "Array" on your screen but if you:
foreach ($my_var AS $val)
{
echo $val."<br />";
}
you'll get:

value1
value2

that's the actual values stored in the array.

Hope to have been more exhaustive than before ;-)

Paolo

 

Featured Threads

Hot Threads This Week

Hot Threads This Month