Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-06-2004, 12:10 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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
metazai is offline   Reply With Quote
Old 05-06-2004, 12:43 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 05-06-2004, 12:47 PM   #3 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Interesting, except I need to POST to an entirely different page.
metazai is offline   Reply With Quote
Old 05-06-2004, 12:59 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
so change your action to that other page .. and then put the php code on the other page.
__________________
Mike
sde is offline   Reply With Quote
Old 05-06-2004, 01:19 PM   #5 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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.
metazai is offline   Reply With Quote
Old 05-07-2004, 03:14 PM   #6 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
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--
Epsilon is offline   Reply With Quote
Old 07-08-2004, 03:39 AM   #7 (permalink)
abbiz
Registered User
 
Join Date: Jul 2004
Posts: 1
abbiz is on a distinguished road
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>";
}

?>
abbiz is offline   Reply With Quote
Old 07-08-2004, 03:57 AM   #8 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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>";
   }

idx is offline   Reply With Quote
Old 07-08-2004, 12:17 PM   #9 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
Wouldn't the "foreach" just get skipped if the size of the array is 0?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-08-2004, 03:23 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 07-08-2004, 06:54 PM   #11 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
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?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-08-2004, 07:06 PM   #12 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 07-09-2004, 06:18 AM   #13 (permalink)
jblasius
Registered User
 
Join Date: Jul 2004
Posts: 1
jblasius is on a distinguished road
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?
jblasius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
EMERGENCY: Dynamic form processing DavH27 PHP 8 10-27-2004 07:52 PM
working with Array Goong MS Technologies ( ASP, VB, C#, .NET ) 3 07-22-2004 12:14 PM
reading a web form for input variables sde PHP 3 08-12-2003 10:02 AM
HTML form preview then INSERT using PHP & MySQL SteveSoler PHP 5 07-07-2002 09:54 AM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 07:14 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting