Log in

View Full Version : html tags in php variables?



TheJoshMan
08-04-2008, 11:27 PM
Is it possible to use html tags in a php variable?

Say if I wanted my variable $body_header to consist of:

<h2>$fname $lname says:</h2><br />

I've tried putting it in the variable like this:



$body_header = "<h2>$fname $lname says:</h2><br />";


But it won't print out. It actually prints out the tags rather than implementing them to format the other variables. Is this possible?

thetestingsite
08-04-2008, 11:44 PM
Are you trying to implement this in the mail form you mentioned in another post? If so, you need to make sure that you are sending the Content-type as text/html or multipart/mixed in the headers of the email. Otherwise, you should be able to just echo $body_header and it will display.

Hope this helps.

TheJoshMan
08-05-2008, 12:42 AM
yes, this will be the "output" of the form once processed via "sendmail.php"

I want to format the data to make it more "readable"

TheJoshMan
08-05-2008, 01:11 AM
Are you trying to implement this in the mail form you mentioned in another post? If so, you need to make sure that you are sending the Content-type as text/html or multipart/mixed in the headers of the email.

Now, how would I go about doing that? I didn't see any section about that in that "headers" page you sent me the link to.

I'm confused. I miss my non-functioning CGI form. LOL

It didn't work, but boy it was so much easier to grasp.

mburt
08-05-2008, 07:42 PM
Yes, HTML tags can be used in strings.
then I don't know about mail.

Read thetestingsite's post. You have to send the Content-type header as text/html.

volunteeer
08-05-2008, 08:04 PM
You have to do something like this:

$from = 'YourName';
$mail_from = 'MyEmail@domain.com';
$mail_to = 'YourClient@domain.com';
$subject = 'subject';
$body = '<html><body>Hello</body></html>';

$headers = "From: $from <$mail_from>\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8;\r\n";
$headers .= "From: $mail_from\r\n";
$headers .= "Reply-To: $mail_from\r\n";
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
mail($mail_to, $subject, $body, $headers, '-t -i -F '.$mail_from.' -f '.$mail_from.'');

Twey
08-06-2008, 05:08 AM
That looks like XHTML, not HTML. I suggest you read http://www.webdevout.net/articles/beware-of-xhtml (this is on my ever-growing list of Things People Should Have to Read Before Beginning Web Development).

TheJoshMan
08-06-2008, 11:10 AM
That was a very informative read. However, this page will be plain old strict HTML 4.01

I have apparently acquired a bad habbit of making page breaks like <br /> instead of <br>

I do appreciate you pointing it out, now I can keep an eye out for it to make sure I don't do it anymore.

TheJoshMan
08-06-2008, 11:10 AM
I haven't quite decided to "jump on" the XHTML bandwagon yet.

Twey
08-06-2008, 09:44 PM
I forgot to mention: if you find yourself putting HTML in strings, chances are you should be breaking out of PHP parsing mode instead. If the output isn't to be sent to the browser directly, consider using an output buffer. A template engine such as Smarty is also a good option, although perhaps a little overkill for this case.

TheJoshMan
08-06-2008, 09:49 PM
thanks for the info, I'll give it a look