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
train
Forum Commoner
Posts: 27 Joined: Wed Aug 16, 2006 3:58 pm
Contact:
Post
by train » Mon Aug 21, 2006 8:22 am
Hello,
I am looking at posted variables like so:
Code: Select all
foreach ($_POST as $key => $value) {
echo "$key has a value of $value<br>";
But it is the values in the array that I am after... and the resultant output:
Code: Select all
custom_12 has a value of Array
first_name has a value of George
middle_name has a value of Samuel
Doesnt help me too much. How can I take a look at what is IN those arrays?
The array custom_12 is a series of checkboxes, and depending on which is checked, I want to send an email to a particular person. It will be a bunch of if statements, I guess... if this box is checked, send a mail to this person and if this box is checked, send an email to this person... and so on...
Thanks!
SteveMellor
Forum Commoner
Posts: 25 Joined: Mon Jun 26, 2006 7:43 am
Post
by SteveMellor » Mon Aug 21, 2006 8:33 am
I think what you might be after is the following.
Assume that I have sent a form with the following field names:
name
address
postCode
now this form is sent as Post data. When the form is recieved by your script you can refer to the variables thus:
$_POST['name']
$_POST['address']
$_POST['postCode']
Any help??
train
Forum Commoner
Posts: 27 Joined: Wed Aug 16, 2006 3:58 pm
Contact:
Post
by train » Mon Aug 21, 2006 8:37 am
SteveMellor wrote: I think what you might be after is the following.
Assume that I have sent a form with the following field names:
name
address
postCode
now this form is sent as Post data. When the form is recieved by your script you can refer to the variables thus:
$_POST['name']
$_POST['address']
$_POST['postCode']
Any help??
I get 'Array' back for 'custom_12' and it IS an array... but I need to see what is IN the array.
Thanks a lot...
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Mon Aug 21, 2006 8:39 am
You can nest foreach loops if your dealing with arrays containing arrays..
Code: Select all
foreach ($_POST as $key => $data)
{
if (is_array($data))
{
foreach ($data as $key => $value) {
echo "{$key}={$value}<br />\n";
}
} else {
echo "{$key}={$data}<br />\n";
}
}
train
Forum Commoner
Posts: 27 Joined: Wed Aug 16, 2006 3:58 pm
Contact:
Post
by train » Mon Aug 21, 2006 8:43 am
astions wrote: You can nest foreach loops if your dealing with arrays containing arrays..
Code: Select all
foreach ($_POST as $key => $data)
{
if (is_array($data))
{
foreach ($data as $key => $value) {
echo "{$key}={$value}<br />\n";
}
} else {
echo "{$key}={$data}<br />\n";
}
}
Now I get
Code: Select all
1=1
2=1
3=1
4=1
5=1
first_name=Chevy
middle_name=
Which is a lot closer... the 2=1 means the second checkbox is the "custom_12" array is checked.
And so I guess if that field is "custom_12" then
"custom_12[2]" would be checked?
But when I try
It doesnt work...
Thanks!
jayshields
DevNet Resident
Posts: 1912 Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England
Post
by jayshields » Mon Aug 21, 2006 8:56 am
You would need to refer to it as $_POST['custom_12'][2] I think, not sure though.
Referring to your original question:
Why not set the value of the checkboxes to the email address you want to send to. It would make things alot easier for you.
You could then take astions code and modify it like so.
Code: Select all
foreach ($_POST as $key => $data)
{
if (is_array($data))
{
foreach ($data as $key => $value) {
mail($value, 'subject', 'message');
}
} else {
echo "{$key}={$data}<br />\n";
}
}
Instead of just knowing that the second checkbox was checked (meaning nothing in terms of application logic), you will know who to send the email to.
train
Forum Commoner
Posts: 27 Joined: Wed Aug 16, 2006 3:58 pm
Contact:
Post
by train » Mon Aug 21, 2006 9:03 am
jayshields wrote: You would need to refer to it as $_POST['custom_12'][2] I think, not sure though.
Referring to your original question:
Why not set the value of the checkboxes to the email address you want to send to. It would make things alot easier for you.
You could then take astions code and modify it like so.
Code: Select all
foreach ($_POST as $key => $data)
{
if (is_array($data))
{
foreach ($data as $key => $value) {
mail($value, 'subject', 'message');
}
} else {
echo "{$key}={$data}<br />\n";
}
}
Instead of just knowing that the second checkbox was checked (meaning nothing in terms of application logic), you will know who to send the email to.
I cant change the checkboxes because they are dynamically generated.
That doesnt work... I still cant read $_POST[custom_12][2]
Thanks though. This is getting me closer.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Mon Aug 21, 2006 9:10 am
To see where everything is..
Code: Select all
echo '<pre>' . print_r($_POST, true) . '</pre>';
train
Forum Commoner
Posts: 27 Joined: Wed Aug 16, 2006 3:58 pm
Contact:
Post
by train » Mon Aug 21, 2006 9:16 am
astions wrote: To see where everything is..
Code: Select all
echo '<pre>' . print_r($_POST, true) . '</pre>';
Yeah, thats really cool. Thanks man.
But still, I need to evaluate the array... so if custom_12[2] is 1, a certain action happens.
I guess I can just
Code: Select all
if (custom_12[1] = 1)
{
(do this);
}
if (custom_12[2] = 1)
{
(do this);
if (custom_12[3] = 1)
{
(do this);
}
And so on...
You guys rock. Thanks!
jayshields
DevNet Resident
Posts: 1912 Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England
Post
by jayshields » Mon Aug 21, 2006 9:55 am
I still cant read $_POST[custom_12][2]
I'm not suprised. I said $_POST['custom_12'][2].
Don't use multiple if's. Use a loop.
Code: Select all
foreach($_POST['custom_12'] as $k => $v) {
//do this
}
train
Forum Commoner
Posts: 27 Joined: Wed Aug 16, 2006 3:58 pm
Contact:
Post
by train » Mon Aug 21, 2006 9:59 am
jayshields wrote: I still cant read $_POST[custom_12][2]
I'm not suprised. I said $_POST['custom_12'][2].
Don't use multiple if's. Use a loop.
Code: Select all
foreach($_POST['custom_12'] as $k => $v) {
//do this
}
The action will vary depending on which member of the array is true, so I dont think the loop will work well.
Thanks. I do appreciate it.
jayshields
DevNet Resident
Posts: 1912 Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England
Post
by jayshields » Mon Aug 21, 2006 10:09 am
You should still use a loop...
Code: Select all
foreach($_POST['custom_12'] as $k => $v) {
if($v == 1) {
//do this
}
}
train
Forum Commoner
Posts: 27 Joined: Wed Aug 16, 2006 3:58 pm
Contact:
Post
by train » Mon Aug 21, 2006 10:10 am
jayshields wrote: You should still use a loop...
Code: Select all
foreach($_POST['custom_12'] as $k => $v) {
if($v == 1) {
//do this
}
}
Will do. Thank you.