I have a form that users don’t have to enter every field. The email that is sent contains a blank space for that field. How can I have an if statement in PHP mail() so that the line isn’t displayed if there is no information to be displayed? I have searched all over and can’t seem to find an answer. Thanks for your help.
I actually have. The tutorials and the books don’t actually talk about what I am referring to. And I don’t even know if what I am asking is possible. What I understand from your post is not what I am asking, so that didn’t explain if it is possible either.
There is nothing in that book, or the two site point php books that explains the question that I am asking, and yes, I have read and worked through both. The w3 school doesn’t have anything on append nor do the three books that I have mentioned.
First of all, is it possible? If so, how can I write an if statement without ending the email. If I end it, I have to rewrite it four times. I could do that, but it seems unnecessary. Certainly there is one small piece that I am missing. I thought that you had something for me, but I don’t understand what you wrote. And going by your source of the w3 school, neither do they.
Is it possible?
ok, it appears that you probably have jumped ahead too quickly.
The link I posted earlier is to the w3schools php tutorials.
On the left hand side menus you will see that sending emails is in their “advanced” section.
My suggestion, for what it’s worth, is to start working sequentially through all the “Basic” tutorials on the left, in parallel to your book, and once you have a good understanding of those, then move into the “Advanced” tutorials.
If you start jumping all over the place trying to do only the things you need before understanding the basics then you will most probably just end up getting confused and tying yourself up in knots.
I got the use of the dot operators out of a book, PHP and MySQL Web Development. I have experimented with not using them, and it works as well. I haven’t come across append. It isn’t on the w3schools site where I have looked. This is my first email in php, so perhaps that should explain some of my confusion as well. I am fairly new to php and have only used it for what I need, so there is plenty that I don’t know yet. Is it possible to include the if statement inside of the mailcontent area? That is where my problem is. I have come close, but the best I can do is get a 1 instead of the line. Obviously, the 1won’t work either. Will I have to write 3if statements for each option (actually four), or can I write one for the one line of code that it is going to affect? I don’t mind researching and reading, but I can’t find anything to help me. If you could point me in a direction, then I would gladly research it, but so far I have coke up empty.
Let’s try this again, I don’t think that you understand my question.
When the above email code is submitted and the Firm (second item) is left blank, this is what the email looks like:
Firstname Lastname
123 Address
Second line of Address
City, State 12345
123-456-7890
hi@email.address
If firm is left blank, would prefer for the email to look like this:
Firstname Lastname
123 Address
Second line of Address
City, State 12345
123-456-7890
hi@email.address
There is no line in between the name and the address where firm goes. Now, either what you suggested doesn’t apply, or I just don’t see how. I got on the forum for the first time hoping that someone could at least point me in the right direction. Since there is more than one option that can be left blank, I don’t want to write an if statement on the entire email. That would require me to write the email many times. If ever it needed to be updated, I would have to update the same content at least four times. What I am asking is if there is a way in the $mailcontent (the message of my mail from my code) to write the code to prevent the blank line from happening.
This is the code I have on the webpage that does just that: <?php if ($Firm != NULL) echo “$Firm <br />”; ?>. I can’t start a new <?php string in the middle of another one (or at least I don’t know how) to write that. When I strip the <?php out of that code and place it in the $mailcontent, the page doesn’t work correctly. That is where my confusion comes in. The w3schools don’t address that particular issue. I can’t find anywhere online that does, and I have searched for it. Perhaps there isn’t a way without rewriting the message many time, but you have led m to believe that there is. My logic also tells me that certainly there is as well.
I tried this:
$mailcontent =
"Your registration information is as follows:
".
"
“.
“”.$First_Name.” “.$Last_Name.”
".
($Firm != NULL) echo “$Firm
“”.$Address.”
“.
“”.$Address2.”
“.
“”.$City.”, “.$State.” “.$Zip_Code.”
“.
“”.$Telephone.”
“.
“”.$Email.”
";
but the echo doesn’t work. I have also tried replacing the Firm line with:
($Firm = NULL. "$Firm
").
But when the $Firm is empty, I get the number 1 instead of nothing. I have tried variations on this, but haven’t been able to come up with anything. Now, without rewriting the message four times, is there anyway in the $mailcontent to get this to happen? At least let me know that, so if it’s not I can stop looking. Otherwise if you don’t want to ‘write the code for me,’ point me to somewhere that I could better understand it. The w3schools don’t have anything that I can find useful. Now, if I am looking in the wrong place, or thinking about this wrong, please guide me in the right direction. I can find my way, and probably am just looking at this wrong. However, your telling me that I am cherry picking ideas and wanting others to write my code is quite offensive. I just need some direction to get going in the right direction.
I would appreciate any help on the matter. Thanks.
That was one of the first places that I went. If I write and if else if statement, there will be at least three with almost the exact same message. The code sample was less than a third of the message, so that would get quite long. There is also another message being sent with similar content.
So perhaps I don’t understand your wording, but I’m not asking you to write it for me. What I am asking is there a way inside of the $mailcontent to write an if statement? When I have tried and I have for over 5 hours, I keep getting mistakes. There is something that I am missing. What you wrote makes zero sense to me. So instead of accusing me of asking you to write the code, perhaps you could rewrite your statement so that it could be better understood.
Your use of the word append doesn’t seem to fit with anything else that I have read today. That is my question. How can I get it in the mailcontent area so that the message doesn’t need to be written four times.
ok, we’re starting to round in circles now because I can only go back to a previous statement as well where I said that I cannot help you anymore without actually writing code because I don’t how to explain what needs to be done in words any clearer than I already have.
You asked for direction so you can do your own research and I pointed you to the w3schools php tutorials.
I cannot help anymore than this - as I said without writing the code.
I can cocantente to items from a database or php code. Quite simple. But I don’t see how that works here. I go back to an earlier statement, what are you suggesting that I do. I don’t know hoe to remove an empty field from an email. I can from a page, which is what the tutorials cover. I didn’t need them for that. I can’t in an email. It leaves a blank line. I want the blank line to be gone. That I don’t need. With what you are telling me, I have found and implemented. Perhaps you have an idea how, but you aren’t helping. How would I concate it before I send it into the email, that’s what I don’t understand from you. You haven’t given any ideas on that.
yes it is and would be no more than a few lines of code.
If you worked through all “basic” tutorials, then I don’t see how you cannot work out how to do it, assuming you understood what the tutorials are teaching.
The tute’s and your book probably don’t mention your specific problem, but your problem is a very common general situation where you just need to check if certain variables are empty or not before you add/append/concatenate/join (or whatever word you prefer to use) them to a string. Whether that string is to be used in an email or for some other purpose is irrelevant.
The tutorials show you how to use the tools to accomplish this task.
without writing the code for you I don’t think there’s anymore I can help with except to suggest you have a look through the IF and IF-ELSE sections in this tutorial which shows you what I mean.
I’m sorry. I don’t understand what you mean. Would this be in the mailcontent or before it? What do you mean before appending it?
ok, all you need to do is check if each parameter is empty or not using an IF statement before appending it to $mailcontent.
If it’s empty, don’t append that parameter.
Here is the code. I have stripped out some to make it easier. The mailcontent is the message that is to be sent.
I want $Firm and $Address2 to be optional in the email. Currently, they are left as blank lines. They need to be in separate lines when the message gets sent.
$mailcontent =
"Your registration information is as follows:
".
"
“.
“”.$First_Name.” “.$Last_Name.”
“.
“”.$Firm.”
“.
“”.$Address.”
“.
“”.$Address2.”
“.
“”.$City.”, “.$State.” “.$Zip_Code.”
“.
“”.$Telephone.”
“.
“”.$Email.”
";
post the code you have so far and I will try to help.
I guess I don’t understand. I have been trying to include an if statement all night. can I put it into the message that is to be sent?
In your script that sends the email, include an IF block that excludes the empty parameter from the form when creating the email’s message body.
I wasn’t accusing you of anything at all. I just said that to help you any further I would have to actually write the code.
The reason I am reluctant to write any code for you is because the questions you are asking and the comments you are making about not understanding what I mean about append is suggesting to me that you didn’t write the code but possibly copied and pasted it from somewhere and now want someone to fix it.
The reason I suspect that, and I could be totally wrong, is because you use the dot operator in your code which is appending the form’s parameters together into the variable
$mailcontent. I don’t see how you can use the dot operator and then claim you don’t understand what I mean by append.