|
 |
|
 |
05-06-2004, 12:10 PM
|
#1 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
|
breaking down an array from a form
Ok, the following question may be a bit simple, but I AM a newbie, after all. I am teaching myself while working on a PHP project mostly, which means I am only learning what I have to learn and which is why I've somehow missed this one and can't figure it out . . .
I know some about how to work with an array from a database table, but how about one from a form? If I have a form with 20 checkboxes using the name "shows", and five of them are checked, that comes through POST with commas, doesn't it? Does PHP automatically read it as an array or do I have to tell it to? Also, once it IS an array, how can I get a php mail script like this:
PHP Code:
$msg="<font face=\"verdana\" size=\"2\">
<strong>First Name:</strong> $firstname<br />
<strong>Last Name:</strong> $lastname<br />";
$to="emailaddress";
$subject="subject";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; ";
$headers .= "charset=iso-8859-1\r\n";
$headers .= "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
mail ($to,$subject,$msg,$headers);
?>
to print only the boxes that were checked?
Any help would be appreciated.
-metazai
|
|
|
05-06-2004, 12:43 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
this is how i embed arrays in a form.
PHP Code:
<form method=post action=<?=$_SERVER['PHP_SELF']?>>
<input type=checkbox name=show[0] value="show 0"> show 0 <br>
<input type=checkbox name=show[1] value="show 1"> show 1 <br>
<input type=checkbox name=show[2] value="show 2"> show 2 <br>
<input type=submit name=submit value=submit>
</form>
<?
if($submit){
foreach($show as $each){
echo $each . " is checked. <br>";
}
}
?>
__________________
Mike
|
|
|
05-06-2004, 12:47 PM
|
#3 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
|
Interesting, except I need to POST to an entirely different page.
|
|
|
05-06-2004, 12:59 PM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
so change your action to that other page .. and then put the php code on the other page.
__________________
Mike
|
|
|
05-06-2004, 01:19 PM
|
#5 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
|
Well, I still couldn't get it to work, but then I changed the name of the array data in my form from "shows" to "shows[]" and then $shows was an array when PHP grabbed it. The "foreach" command now works on it perfectly, so thanks.
|
|
|
05-07-2004, 03:14 PM
|
#6 (permalink)
|
|
Regular Contributor
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
|
Quote:
Originally posted by metazai
Well, I still couldn't get it to work, but then I changed the name of the array data in my form from "shows" to "shows[]" and then $shows was an array when PHP grabbed it. The "foreach" command now works on it perfectly, so thanks.
|
One note one this: if no boxes in the form are checked PHP will choke on the foreach command because it's not a valid array. I think the same might be true if only one box is checked, but I don't recall for sure. Anyway, you'll want to check the variable in your code first, maybe with is_array(), before you do a foreach.
__________________
--Epsilon--
|
|
|
07-08-2004, 03:39 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Jul 2004
Posts: 1
|
I didn't got the code working properly. I have added a line, so now it works fine. /Albin
<?$shows=$_REQUEST['shows'];?>
<form method=post action=<?=$_SERVER['PHP_SELF']?>>
<input type=checkbox name=shows[] value="show 0"> show 0 <br>
<input type=checkbox name=shows[] value="show 1"> show 1 <br>
<input type=checkbox name=shows[] value="show 2"> show 2 <br>
<input type=submit name=submit value=submit>
</form>
<?
foreach($shows as $each){
echo $each . " is checked. <br>";
}
?>
|
|
|
07-08-2004, 03:57 AM
|
#8 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
You may want to add a small check before the loop.
eg:
PHP Code:
if (sizeof($shows) > 0) {
foreach($shows as $each){
echo $each . " is checked. <br>";
}
}
|
|
|
07-08-2004, 12:17 PM
|
#9 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
Wouldn't the "foreach" just get skipped if the size of the array is 0?
|
|
|
07-08-2004, 03:23 PM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
no, it will generate an error if the variable is not declared as an array.
for example:
PHP Code:
<?
// bad, this will generate an error
// since $myarray as not been declared as an array
foreach($myarray as $each){
// do something
}
// this next one is ok because
// php knows that $myarray is an array
// now it will skip the foreach and not generate an error
$myarray = array();
foreach($myarray as $each){
// do something
}
?>
__________________
Mike
|
|
|
07-08-2004, 06:54 PM
|
#11 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
Ah, so it's not that the array is empty, but that it's not declared. Can you run PHP stuff through a syntax checker or compiler-type program to catch that kind of stuff?
|
|
|
07-08-2004, 07:06 PM
|
#12 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
Quote:
Originally posted by Belisarius
Wouldn't the "foreach" just get skipped if the size of the array is 0?
|
Yes, it will skip the foreach. No clue if that's the desired behavior. (note the docs on sizeof/count - they work with strings, but only to return 1 if the string has data vs the amount of array slices)
So the previous example would pass the sizeof() test, but may bomb out on the foreach as sde mentions. Adding an additional check with is_array() might be wise as well. I believe Epsilon is correct about the `one checkbox` thing. Its been awhile, but I remember having to add some additional checks that detected data and if the var was an array. If it had data, but no array, then I'd have to manually assign the data to slice 0. Maybe it was an old php thing - maybe not.
To be as clean as sde's example, you may have to do something like:
PHP Code:
// create local var - its never clean to rely on register_globals
$shows = Array();
if (sizeof($_POST['shows']) > 0) {
if (is_array($_POST['shows'])) {
$shows = $_POST['shows'];
}
else {
$shows[0] = $_POST['shows'];
}
foreach ($shows as $foo) {
//bar
}
}
-r
|
|
|
07-09-2004, 06:18 AM
|
#13 (permalink)
|
|
Registered User
Join Date: Jul 2004
Posts: 1
|
I have been trying to use this, but have been unsuccessful. When I follow your example, all I get into the $shows array is 'Array'. One difference is that I am using my MySQL id field (primary key) as the values for the checkbox array. Any thoughts?
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:57 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|