Help Me! Plz? Multidimensional Arrays to Email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

Ok i have written the php to email everything on my html form execpt this so i will show you what i have and then if anyone could be so kind as to show me the correct way of doing this, keep in mind that i have only been doing php for 1 month and learned from the php5 and mysql bible.

Code: Select all

<form action="Sell_agency.php" method="post" name="SellAgency" onSubmit="return validator(this)">
  <table width="929" height="244">
          <TR>
            <TD width="86" height="40" ALIGN="right"><label for="Contact_FullName">Contact Name:</label></TD>
            <TD width="276"><INPUT NAME="Contact_FullName" type="text" SIZE=45>            </TD>
          </TR>
          <TR>
            <TD height="24" ALIGN="right"><EM>Title</EM></TD>
            <TD><Input NAME="Contact_Title" type="text" SIZE=35>            </TD>
          </TR>
          <TR>
            <TD height="24" ALIGN="right"><EM>Organization</EM></TD>
            <TD><INPUT NAME="Contact_Organization" type="text" SIZE=45>            </TD>
that is the sample of the normal html fields im using, which im not having problems with but what im having problems with is

Code: Select all

<input type="Checkbox" name="boxes[1]" value="Alabama"/>
                <span class="style3"> ALABAMA<br>
                <input type="Checkbox" name="boxes[2]" value="Alaska"/>
                  ALASKA <br>
                  <input type="Checkbox" name="boxes[3]" value="Arizona"/>
                  ARIZONA <br>
I cant seem to get them to work right, i have 51 checkboxes (50 states and washington dc) i have my php form handler set up to email me the info from these forms and it looks like this

Code: Select all

<?php
 
$Contact_FullName = $_POST['Contact_FullName'];
$Contact_Title = $_POST['Contact_Title'];
$Contact_Organization = $_POST['Contact_Organization'];
$Contact_WorkPhone = $_POST['Contact_WorkPhone'];
$Contact_FAX = $_POST['Contact_FAX'];
$Contact_Email = $_POST['Contact_Email'];
$Contact_URL = $_POST['Contact_URL'];
$additional_info = $_POST['additional_info'];
$bail_volume = $_POST['bail_volume'];
$boxes = $_POST['boxes'];
 
$formsent = mail('info@somerandomsite.com',
'Somerandomsite Sell Agency',
"Request From: 
Name: $Contact_FullName\r\n
Title: $Contact_Title\r\n
Organization: $Contact_Organization\r\n
WorkPhone: $Contact_WorkPhone\r\n
FAX: $Contact_FAX\r\n
Email: $Contact_Email\r\n
URL: $Contact_URL\r\n
Additional Info: $additional_info\r\n
Bail Volume: $bail_volume\r\n
States: $boxes\r\n",
 
"From: $Contact_Email\r\n Bounce-to: info@somerandomsite.com");
   if ($formsent) {
  echo "<P>Hi, $Contact_FullName, We have received your request, we will respond to you as soon as possible.
Thank You,
somerandomsite.com";
} else {
   echo "I'm sorry $Contact_FullName, but there seems to be a problem, please try to resubmitt the form.";
}
 
?>
at this moments it will only send one state and not all that r chosen so....
im in a tight spot, i dont know what i should do at this point, i looked up multidimensional arrays but im not sure how to incorporate them into my current form handler so i could use a hand here.

i know this is probable a noob thing but like i said i just started using php a month ago and learned it from a book so plz go easy on me and i would appreciate and help without any flaming.

thanks in advance

Innervate
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by andym01480 »

Code: Select all

$boxes=array($_POST['boxes']);
Then use a foreach loop to cycle through the $boxes array.
I'd use an if statement to check whether each one is checked and add it if it is.

Have a go at coding that and come back!
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

im not sure what u mean could u show me where to put that in the code i have and any other changes i need to make with the samples i included

im a newb sorry

thanks again in advance

Innervate
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by andym01480 »

Code: Select all

//firstly grab all the form variables and put them in an array called $form
foreach ($_POST as $varname=>$value) {
$form[$varname]=$value;
if(get_magic_quotes_gpc()) $form[$varname]=stripslashes($form[$varname]); //gets rid of slashes if magic quotes is on
}
 
//make variables for the mail() call
$to='info@somerandomsite.com';
$subject="blah blah";
$headers="From: $Contact_Email\r\n Bounce-to: info@somerandomsite.com\r\n";
 
//Build $message
$message=' Request From:';
$message.= "Name: $form['Contact_FullName']\r\n";
$message.="Title: $form['Contact_Title']\r\n";
//etc etc - fill the other form values
 
//build in states bit
 $states=array();
foreach ($form['boxes'] as $varname=>$value) {
if($value!='') $states[]=$values;
}
$list=implode(', ',$states); //Lists the selected states with a comma between
$message.='States: '.$list.'\r\n';
 
mail($to,$subject,$message,$headers) or die ("There was a problem sending the email");
I haven't tested it, but I think it will work
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

ok i think i figured out what u were talking about and u helped me with the wrong part.

that part i need help with is the

Code: Select all

$formsent = mail('info@alphamerchantservices.com',
'BailAgentsonly Sell Agency',
"Request From: 
Name: $Contact_FullName\r\n
Title: $Contact_Title\r\n
Organization: $Contact_Organization\r\n
WorkPhone: $Contact_WorkPhone\r\n
FAX: $Contact_FAX\r\n
Email: $Contact_Email\r\n
URL: $Contact_URL\r\n
Additional Info: $additional_info\r\n
Bail Volume: $bail_volume\r\n
States: $boxes",
 
specifically the part with the states: $boxes",

i need to email the data of the array not post to the db

thanks

Innervate
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

ok i see thanks a bunch ill try that
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

ok tried it and this is what i got:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Sell_agency.php on line 107


and line 107 is

Code: Select all

$message.= "Name: $form['Contact_FullName']\r\n";
$message.="Title: $form['Contact_Title']\r\n";
$message.="Title: $form['Contact_Organization']\r\n";
$message.="Title: $form['Contact_WorkPhone']\r\n";
$message.="Title: $form['Contact_FAX']\r\n";
$message.="Title: $form['Contact_Email']\r\n";
$message.="Title: $form['Contact_URL']\r\n";
$message.="Title: $form['additional_info']\r\n";
$message.="Title: $form['bail_volume']\r\n";
i think it will work out ok if i can figure out the line error
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by andym01480 »

Sorry. Put

Code: Select all

{$form['blah']}
-php needs the curly brackets to id the variables.
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

that didnt work either, im getting the same error
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by andym01480 »

Post a couple of lines before 107 and 107 please
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

im so sorry it did change, i did

Code: Select all

$form['blah']}
 
instead of

Code: Select all

{$form['blah']}
 
but now im geting a new error

Warning: Invalid argument supplied for foreach() in /Sell_agency.php on line 121

and line 121 is

Code: Select all

foreach ($form['boxes'] as $varname=>$value) {
if($value!='') $states[]=$values;
}
$list=implode(', ',$states); //Lists the selected states with a comma between
$message.='States: '.$list.'\r\n';
 
mail($to,$subject,$message,$headers) or die ("There was a problem sending the email");
 
so im not sure if i have to do something to the html form or if its something in the php thats off

sorry im such a newb and need to be spoon fed
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by andym01480 »

I'm probably being dense too! It's late here in the UK
Replace

Code: Select all

 
 foreach ($form['boxes'] as $varname=>$value) {
if($value!='') $states[]=$values;
}
$list=implode(', ',$states); //Lists the selected states with a comma between
$message.='States: '.$list.'\r\n';
 
mail($to,$subject,$message,$headers) or die ("There was a problem sending the email");
with

Code: Select all

print_r($form); //see what's happening
/*
 foreach ($form['boxes'] as $varname=>$value) {
if($value!='') $states[]=$values;
}
$list=implode(', ',$states); //Lists the selected states with a comma between
$message.='States: '.$list.'\r\n';
 
mail($to,$subject,$message,$headers) or die ("There was a problem sending the email");
*/
and post what comes out. (Or someone else will tell you what I've done wrong in the meantime!!!)
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

its 5pm here in texas,

(no i dont wear a cowboy and and carry a revolver... lol, but i probably have an accent that i dont notice)

but im not getting a different error

Array ( [Contact_FullName] => anthony [Contact_Title] => me [Contact_Organization] => me [Contact_WorkPhone] => 4696930277 [Contact_FAX] => 4696930277 [Contact_Email] => me@u.com [Contact_URL] => u.com [boxes] => Array [Annual_bail_Volume] => $100,000 to $250,000 [Additional_Info_or_Comments] => me [submit_request] => Submit Form )
Warning: Invalid argument supplied for foreach() in Sell_agency.php on line 123


and line 123 is the part where the foreach ($form['boxes'] is

just out of curiousity on my form should i put the states list like

Code: Select all

<input type="Checkbox" name="boxes[1]" value="Alabama"/>
                <span class="style3"> ALABAMA<br>
or like

Code: Select all

<input type="Checkbox" name="boxes" value="Alabama"/>
                <span class="style3"> ALABAMA<br>
coolbung
Forum Newbie
Posts: 8
Joined: Mon Feb 18, 2008 9:37 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by coolbung »

Hello Innervate,

A few things

1. All the HTML code should always be in lowercase. (This applies to the type & name attributes, the value may contain uppercase). This is not an error but, good coding practice.
2. It is also good practice to name all your variables in a suggestive manner. Like states[] instead of boxes[]
3. Try removing the numbers in your checkbox name.

Code: Select all

<input type="Checkbox" name="boxes[1]" value="Alabama"/>
should be

Code: Select all

<input type="checkbox" name="boxes[]" value="Alabama"/>
After this, in the part of your form where you want to output the state names, you can do something like

Code: Select all

foreach ($form['boxes'] as $state) {
echo "{$state}\n";
}
Also make sure you actually tick some of the boxes!!

If this dosent work,
try doing
print_r($_POST);
print_r($form)

to see if the date is being sent or not. In case it appears in $_POST but not in $form you need to check your cleaning code
Innervate
Forum Newbie
Posts: 11
Joined: Sun Feb 17, 2008 11:05 am

Re: Help Me! Plz? Multidimensional Arrays to Email

Post by Innervate »

ok this is what i got

Array ( [Contact_FullName] => Anthony [Contact_Title] => me [Contact_Organization] => me [Contact_WorkPhone] => 4696930277 [Contact_FAX] => 4696930277 [Contact_Email] => me@u.com [Contact_URL] => u.com [boxes] => Array ( [0] => District Of Columbia [1] => Hawaii [2] => Nevada [3] => Tennessee ) [Annual_bail_Volume] => $100,000 to $250,000 [Additional_Info_or_Comments] => me [submit_request] => Submit Form ) Array ( [Contact_FullName] => Anthony [Contact_Title] => me [Contact_Organization] => me [Contact_WorkPhone] => 4696930277 [Contact_FAX] => 4696930277 [Contact_Email] => me@u.com [Contact_URL] => u.com [boxes] => Array [Annual_bail_Volume] => $100,000 to $250,000 [Additional_Info_or_Comments] => me [submit_request] => Submit Form )
Warning: Invalid argument supplied for foreach() in /Sell_agency.php on line 113

and line 113 is

Code: Select all

foreach ($form['boxes[]'] as $state) {
echo "{$state}\n";
}
but its showing all the states in that array its just not sending it and im getting the same argument error on the foreach()
Post Reply