FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Using an if statement inside a string variable





miteY
I'm trying to send a form's result using php mail() function and I am having a problem adding the input from checkboxes

Code:

$message = 
'<html> ... <ul>' ;
if ($varA == 1 ) echo '<li>Var A</li>';
if ($varB == 1 ) echo '<li>Var B</li>';
if ($varC == 1 ) echo '<li>Var C</li>';

echo '<ul>. . . ';

$headers = 'header info';
$to = '$email';
$subject = 'Email Subject';

mail($to, $subject, $message, $headers);



The code is not working, so can someone please tell me what I'm doing wrong?
IceCreamTruck
Try this and see if it helps you understand your problem here. I'm glad you've asked because you are sending echo requests to the browser not building your message variable as you think is happening. Note when you "echo" it is not going to "message" variable

Check my line notes

Code:


<?php

//$message = '<html> ... <ul>' ;
// Do this instead
$message = '<html> ... <ul>\n' ;

// try adding to the message based on variables
if ($varA == 1 ) {
   $message += '<li>Var A</li>';
} else if ($varB == 1 ) {
   $message += '<li>Var B</li>';
} else if ($varC == 1 ) {
   $message += '<li>Var C</li>';
}

/*  Don't use this old portion...
$message = '<html> ... <ul>' ;
if ($varA == 1 ) echo '<li>Var A</li>';
if ($varB == 1 ) echo '<li>Var B</li>';
if ($varC == 1 ) echo '<li>Var C</li>';
*/

// Make sure you close this UL tag </ul>
//echo '<ul>. . . ';
$message += '</ul>. . . ';


$headers = 'header info';
$to = '$email';
$subject = 'Email Subject';

mail($to, $subject, $message, $headers);

?>

miteY
IceCreamTruck thanks for the reply.

But the if and elseif statements in your code will only allow the value of one checkbox to be added the added. How can I overcome this?

Code:

if ($varA == 1 ) {
   $message += '<li>Var A</li>';
} else if ($varB == 1 ) {
   $message += '<li>Var B</li>';
} else if ($varC == 1 ) {
   $message += '<li>Var C</li>';
}
sonam
If I good understand your question there is two way to solve your problem - Ajax with PHP or pure PHP.
EDIT:
This upper is wrong. I was understand you need select input and after reading again I get it you need checkbox.

Quote:
But the if and elseif statements in your code will only allow the value of one checkbox to be added the added. How can I overcome this?


If you need help with mail form and sending then you can read my tutorial:
http://www.frihost.com/users/sonam/blog/vp-83950.html

The simplest way to solve your problem is to use different names (with same part) and different values for different checkbox.

Code:
<form action="sender.php" method="post">
<input type="checkbox" name="vehicle1" value="Bike"  /> Bike<br />
<input type="checkbox" name="vehicle2" value="Car"  /> Car <br />
<input type="checkbox" name="vehicle3" value="Truck"  /> Truck<br />
<input type="submit" name="send" value="SEND" />
</form>


Then in sender.php just need to grab all posted (checked) values.
For example print_r will look like this:
Code:
Array ( [vehicle1] => Bike [vehicle2] => Car [send] => SEND )


Now you can with foeach function separate your inputs.
Code:
foreach($_POST as $key => $val) {
   if(strpos($key, 'vehicle') !== FALSE) {
      $message = @$message . $val . ', ';
   }
}



Sonam
IceCreamTruck
Code:

if ($varA == 1 ) {
   $message += '<li>Var A</li>';
   $message += '<li>Var B</li>';
   $message += '<li>Var C</li>';
} else if ($varB == 1 ) {
   $message += '<li>Var D</li>';
   $message += '<li>Var E</li>';
   $message += '<li>Var F</li>';
} else if ($varC == 1 ) {
   $message += '<li>Var G</li>';
   $message += '<li>Var H</li>';
   $message += '<li>Var I</li>';
}


Sonam is a good coder, I'm happy he's here to support. Sonam, I'm just showing where his problem was... I'm not even sure why he's sending html in email, or if his expected outcome is what he wants, but Ajax may be a bit much at the moment.

Please detail what you are trying to accomplish. As you can see there is always several ways to accomplish something, and it will help us help you if we know what you are trying to make as the outcome of your script here, or show us the live example.

The above example would print three <li></li> every time whereas the first code I sent you only prints once <li></li> based on a condition string: A == 1, or B == 1, or C == 1 ... but with my code you run into the problem if A is 1 and C is 1 then it only prints A. This is why we need to know what you are doing, so I know better how to handle the variables and conditions of what you are trying to accomplish here.
sonam
@IceCreamTruck
You are right. We haven't enough information what he want to do. I am just stick on his sentence...
Quote:
But the if and elseif statements in your code will only allow the value of one checkbox to be added the added. How can I overcome this?


...but maybe this is also wrong way. BTW I sow some of your help in coding forums and it is great, too.

Sonam
IceCreamTruck
Actually Sonam, I'd like to get your Ajax tutorial. I want to do rails.... push info to the browser without user hitting refresh. Do we need a server side service for that?
sonam
IceCreamTruck wrote:
Actually Sonam, I'd like to get your Ajax tutorial. I want to do rails.... push info to the browser without user hitting refresh. Do we need a server side service for that?


I haven't my ajax tutorial. I am just using ajax for sending info to php script and getting info from same file. I was learn this here:
http://www.w3schools.com/ajax/ajax_aspphp.asp

Of course, after some time it is possible to insert some other things but I am looking for solution when I need it not before. Embarassed

Sonam
IceCreamTruck
OK, I almost left the tutorial because I thought it relied on ASP, and I don't use ASP, and I'm not going to start. I really don't like ASP, and it may be because bad coders, but honestly I've seen some consistently bad ASP over the years and it always seems like the biggest mess in the world to clean it up, and even after cleanup developers are still reporting a lot of bugs. I'm with php all the way now.

It's a great example, and I'm going to use it. I think I can see how this will help, and actually using body.innerHTML you could use this to rail out a completely new page to the browser, i think. I haven't dug into it that deep, but I think this is what I was looking for, so thanks!
sonam
You are right you can change full page if you want but my suggestion is not to change directly body tag. It is better to insert some main div and then change content of this div. Good luck with learning and ajax.

Sonam
IceCreamTruck
Yeah... I was just giving example.

Also, have you noted the innerHTML bug where IE doesn't make <A HREF=""> tags a link? Pretty frustrating. Forced me to go back and make code changes.

It's also something Microsoft and Apple have in common. They are so stealing technology from each other STILL.
miteY
I know it's been very long since I made this post, but I feel a proper solution was not provided here. I have been away for a long time if not I would have posted this before now.

Firstly, I want to thank all those who replied to my initial post and tried to help

The solution I applied to get the values of the checkboxes independent of each other was to use if statements without else or elseifs to determine if each checkbox is checked, and then add its value to the message. This takes away the complexity and for me got the job done.

Code:

$message = '<html> ... <ul>' ;

if ($varA == 1 ) {$message += '<li>Var A</li>';}

if ($varB == 1 ) {$message += '<li>Var B</li>';}

if ($varC == 1 ) {$message += '<li>Var C</li>';}

$message += '</ul>. . . ';

$headers = 'header info';

. . .
Related topics
[Beginner] Echoing
Problem with using Dev-C++ Editor
Random picture in Flash..
OLD post: Please delete
Flatforum problem(NOT the module from drupal CMS)
[*RESOLVED*]Please close for me
neep help making email form that accepts html tags
array check box on php script using $_POST
Guitar Thread: 7 Strings Guitars (or possibly more)
PHP script problem
HELP, reading text file from USB drive in visual basic
Why Perl isn't easy to learn
The Choice of Belief
Alerts -Website Inventory
Reply to topic    Frihost Forum Index -> Webmaster and Internet -> Design Tips

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.