Jump to content

PHP mail - checkboxes values do not appears in email


fazlionline

Recommended Posts

From http://www.computing.net/answers/webdevel/php-amp-checkboxes/1122.html

 

Remember that checkboxes with the same name work in a group, and the form will pass their value as an array. However, in your form you should tell explicitly that the checkboxes should pass their value as an array.

 

For example:

 


 

So in your case your script should loop through all the elements in the area to see if the box was checked or not.

 

For example:

 

$state=$_POST['state'];
foreach ($state as $statename)
{
echo "$statename is checked";
}
?>

 

Basically, the important bits are that you need to add "[]" to the end of your checkbox's name, so that PHP understands that it's dealing with an array of values, and then use a foreach loop to get each of those values within the PHP script.

Link to comment
Share on other sites

You weren't really supposed to simply copy/paste the code -- it contains the method you need to follow to get this working, not the solution itself. If you simply copy and paste everything, rather than understanding how it works, how are you going to learn?

 

The checkboxes are already in a variable -- you just need to loop through and add them to your email. For example:

 

$details = "\n URL - Form: $url\n
           Subject: $subject\n
           Name: $name\n
           email: $email\n
           system: $system \n
           gender: $gender \n
           language: ";
foreach ($language as $lang)
{
   $details .= "$lang ";
}
$details .= "\n";
$details .= "message: $message \n";

Link to comment
Share on other sites

thanks i read PHP foreach and PHP array in TIZAG.com

thanks alot.

 

now, i have two qestions:

 

1 - if a user do not click any of the optoin in langauges, i get error:

 

Warning: Invalid argument supplied for foreach() in d:\Customers\user1092190\www\t6_e\contactus.php on line 21

 

Mail sent! Yay PHP!

 

2 - as i am working on Job Application Form, i need such more checkboxes group.

for example, this one was for language, other may be for Interesets (Interest: PHP, JAVA, CSS), other may be for somthing else.

so how i can make all together in my above PHP scrip?

 

thanks

Edited by fazlionline
Link to comment
Share on other sites

1) Try replacing the foreach loop with this:

 

if (isset($language) && $language != '') {
foreach ($language as $lang)
{
   $details .= "$lang ";
}
}

 

I believe that should fix the error, though I haven't tested it.

 

2) You just need to repeat what I've shown you to do. Create other checkboxes that share the same name and include "[]" at the end of their name so PHP realizes they are an array, get the checkbox values using $_POST or $_REQUEST, and then loop through each the checkbox values using a foreach loop to add them to your $results variable so it gets emailed to you.

Link to comment
Share on other sites

Thanks, now I do not get error if all checkboxes are left blank.

 

I have added second group, interests.

It also works fine but I get languages and interests values all in one line in my email.

 

I have changed the code to this:

if (isset($language) && $language != '') {foreach ($language as $lang){ $details .= "$lang - " ;}}

$para = "

";

$para;

if (isset($interest) && $interest != '') {foreach ($interest as $int){ $details .= "$int - ";}}

 

But didn?t worked

 

How I can get them to appear each group in each line.

 

Like

...

Languages: English - Pashto - Dari

Interests: PHP - Java - CSS

...

 

 

 

My HTML Code is:

 

Languages:

 

English

Pashto

Dari

 

 

 

Interests:

 

PHP

Java

CSS

 

 

Thanks

Edited by fazlionline
Link to comment
Share on other sites

thanks alot.

can i store the value returend by foreach in a variable?

 

foeexample:

 

if (isset($interest) && $interest != '') {foreach ($interest as $int){    $details .=  "$int - ";}}
$SelectedInterests = $int ;

 

becuse i want to add this varibale in $details :

 

...
$details = "\n URL - Form: $url\n
           Subject: $subject\n
           Name: $name\n
           email: $email\n
           system: $system \n
           gender: $gender \n
           message: $message \n
           [b][color=red]Interest: $int \n[/color][/b]
           language: ";
...

 

 

thanks

Edited by fazlionline
Link to comment
Share on other sites

You could. Something like this should work (placed in the area of your code that contains "$language = $_REQUEST['language'] ; $message = $_REQUEST['message'] ;" etc...):

 

$selectedInterests = '';
$interest = $_REQUEST['interest'] ; //this assumes that your checkboxes are named "interest[]"
if (isset($interest) && $interest != '') {foreach ($interest as $int){    $selectedInterests .=  "$int - ";}}

 

and then in when you create the details variable:

 

$details = "\n URL - Form: $url\n
           Subject: $subject\n
           Name: $name\n
           email: $email\n
           system: $system \n
           gender: $gender \n
           message: $message \n
           Interest: $selectedInterests \n
           language: ";

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...