Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
This forum does not support [code] tags, so just post your PHP as normal text. HTML is more difficult. You either need to replace the greater than and less than characters with the escaped code (&)lt; or use replacement characters such as the square brackets [].
Another difficulty is that text is trimmed so that any spacing added to code to make it more readable is lost as well.
Michael J

Michael,
He shouldn't have to do that, so long as he is posting HTML this forum doesn't support. For example, he would have to do [a href="test"]test[/a] but he could do <script language="javascript"></script> fine. The best way to make sure the code is OK is to look at the preview of the post.
Justin

Well, let's try this, then. I'm trying to build a form the contents of which will be submitted to me via email. This PHP is doing basically what I want it to do. Question 1: If I wanted to give it some color or style, etc. could I just enclose the PHP part in the standard HTML tags? Second, if you'd like to critique this PHP, I would welcome any feedback. I'm totally new to PHP, so I can use all the pointers I can get, shortcuts, clean-ups, etc. And now for my code:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = stripslashes($_POST['subject']);
$message = "Name: " . stripslashes($_POST['name']) . "<br />";
$message .= "Email: " . stripslashes($_POST['email']) . "<br />";
$message .= "Message: " . stripslashes($_POST['message']) . "<br />";
$message .= "Comments: " . stripslashes($_POST['comments']) . "<br />";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email";mail( "me@earthlink.net", "Subject: $subject",
$message, $headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mail3.php'>
Email: <input name='email' type='text' /><br />
Name: <input name='name' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='10' cols='40'>
</textarea><br />Comments:<br />
<textarea name='comments' rows='10' cols='40'>
</textarea><br /><input type='submit' />
</form>";
}
?>

Here goes:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = stripslashes($_POST['subject']);
$message = "Name: " . stripslashes($_POST['name']) . "<br />";
$message .= "Email: " . stripslashes($_POST['email']) . "<br />";
$message .= "Message: " . stripslashes($_POST['message']) . "<br />";
$message .= "Comments: " . stripslashes($_POST['comments']) . "<br />";// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email";mail( "me@earthlink.net", "Subject: $subject",
$message, $headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{// CLOSE PHP TAG, PROCEED WITH HTML
// PURE HTML BELOW
?>
<form method='post' action='mail3.php'>
Email: <input name='email' type='text' /><br />
Name: <input name='name' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='10' cols='40'>
</textarea><br />Comments:<br />
<textarea name='comments' rows='10' cols='40'>
</textarea><br /><input type='submit' />
</form>
<?php
// START PHP TAG AGAIN BECAUSE
// THE PHP ABOVE IS NOT FINISHED
// (The if-else statement)
// (the enclosing curly bracket below will finish it)}
?>
-----
Now you can add styling easily to the HTML part.
Actually, you can also add styling to your original code, but you need more things to consider as the HTML will not be ouputted normally because it will be parsed by PHP first. (The "echo" part, which is no longer exists in my sample code).
If it worked well, you can try fixing the "thank you" part on your own.
Try to change this:
echo "Thank you for using our mail form";
So that it will output some colorful text and/or box either by using my method above (by ending and starting PHP) or by styling it directly inside the echo.
---
Fubar

Laler,
thanks for the tips, I'll work it. Above the first incedence of
PHP, should I include a DTD and the html, head, body tags as
normal? I guess I would have to (to stye it), wouldn't I? I'm
also big on validation. Will html w/ included PHP cause the
validator grief? Guess there's at least one way to find out,
build it and send it to the validator!

Yes you can (you must if you care about standards) put them inside the <body> tag.
PHP don't affect the web-standards.
PHP is "outputting HTMLs, by pre-process"... So you can create "dynamic content".
Using PHP, a same page can show an HTML form, or the word "thank you for submitting the form". This is not possible wuth pure HTML.
-----
I think the HTML form isn't valid. I think attributes must be enclosed with double quote, unless you're using using HTML 3 doctype or below.
---
Fubar

Damn, I wrote a nice long response last night and apparently didn't confirm before shutting down.
Anyway, there is a problem with the process above. The way the code works now, if the user enters the email address the script "assumes" that the user entered all the other necessary fields. You need to add some additional validation to ensure that subject and message or content have values - otherwise what's the point of sending a blank email?
Michael J

Yes, the whole code seems to be done in a hurry :D (or made long time ago)
About web-standards, other than the double quote I mentioned above, in XHTML (at least 1.1) form fields must be enclosed with a container (div, p, etc).
---
Fubar

http://www.perfectionconstruction.c...
Here's where I'm at today. I'll post the code below.
Laler, last I checked this page validates! Yay!! "the whole code seems to be done in a hurry :D (or made long time ago"
Right and right, found it and modified it from three different sources(tuts) on the net.
Michael J.: Yes, I wondered about that, too. How do I check for (validate?) the presence of content in the other fields? Same isset type function as to check for the email value (pardon the terminology if I'm wrong)?
Now for the updated, validating-like code:
(hope it doesn't get mangled in the post)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="author" content="Robert F. Salazar" />
<meta name="description" content="ARP" />
<meta name="keywords" lang="en-us" content="APR" /><title>Form using PHP to submit to email</title>
<meta http-equiv="content-type" content="text/xhtml; charset=ISO-8859-1" />
<style type= "text/css">
</style>
</head>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = stripslashes($_POST['subject']);
$message = "Name: " . stripslashes($_POST['name']) . "<br />";
$message .= "Email: " . stripslashes($_POST['email']) . "<br />";
$message .= "Message: " . stripslashes($_POST['message']) . "<br />";
$message .= "Comments: " . stripslashes($_POST['comments']) . "<br />";// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email";mail("robazar@earthlink.net", $subject,
$message, $headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{// CLOSE PHP TAG, PROCEED WITH HTML
// PURE HTML BELOW?>
<h1>Personal Information</h1>
<form method="post" action="mail4.php">
<div>Name: <input name="name" type="text" /><br /><br />
Email: <input name="email" type="text" /><br /><br />
Subject: <input name="subject" type="text" /><br /><br />
Message:<br />
<textarea name="message" rows="5" cols="40">
</textarea><br /><br />Comments:<br />
<textarea name="comments" rows="5" cols="40">
</textarea><br /><br /><input type="submit" value="Please Submit" />
<input type="reset" value="Reset Form" />
</div>
</form><?php
// START PHP TAG AGAIN BECAUSE
// THE PHP ABOVE IS NOT FINISHED
// (The if-else statement)
// (the enclosing curly bracket below will finish it)}
?>
</body>
</html>

"found it and modified it from three different sources"
Ah, so you modified it! I thought you just copy paste it from some (old/hurry) tutorial. =D
To validate in PHP:
if (
isset($_REQUEST['email']) &&
isset ($_REQUEST['name']) &&
isset ($_REQUEST['otherfieldstovalidate'])
)
validate using PHP is the foolproof method, but takes longer time. You can validate the form by using JavaScript so it is faster for the user.PHP:
Submit → HTTP POST → validationJavaScript:
Submit → validation → HTTP POST
---
Fubar

on a second thought, I think the better way is:
if (
!empty ($_POST['email']) &&
!empty ($_POST['name']) &&
!empty ($_POST['otherfieldstovalidate'])
)
Try to submit your form without inputting anything, It will still be submitted, because the $_REQUEST['email'] is (always) set.So you need to test if it is empty or not.
www.php.net/isset
www.php.net/empty---
Fubar

I'll look into it. I'm thinking this isset thing doesn't work, if
you don't fill out anything on the form and click submit, it
gives you the thank you page anyway, it doesn't display the
form again. Try it. And it sends an email message anyhoo,
but with no data in the fields and a weird "from" field. I'll
play with the !empty thing.

If I use !empty, I get the Thank You echo as soon as the page launches, but get no email...
I tried it with "empty" as the function (no ! before empty)if (
empty ($_POST['email']) &&
empty ($_POST['name']) &&
empty ($_POST['message'])
)and it works better but not completely; that is, if you fill in just one field, it'll run the mail function. But I'm trying to test for MULTIPLE empty fields. I'm missing something simple here, I'm sure. Care to take a peek?
http://www.perfectionconstruction.c...
And the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="author" content="Robert F. Salazar" />
<meta name="description" content="ARP" />
<meta name="keywords" lang="en-us" content="APR" /><title>Form using PHP to submit to email</title>
<meta http-equiv="content-type" content="text/xhtml; charset=ISO-8859-1" />
<style type= "text/css">
</style>
</head>
<?php
$email = $_REQUEST['email'] ;
$subject = stripslashes($_POST['subject']);
$message = "Name: " . stripslashes($_POST['name']) . "<br />";
$message .= "Email: " . stripslashes($_POST['email']) . "<br />";
$message .= "Message: " . stripslashes($_POST['message']) . "<br />";
$message .= "Comments: " . stripslashes($_POST['comments']) . "<br />";// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email";
?>
<body><h1>Personal Information</h1>
<form method="post" action="mail5.php">
<div>Name: <input name="name" type="text" /><br /><br />
Email: <input name="email" type="text" /><br /><br />
Subject: <input name="subject" type="text" /><br /><br />
Message:<br />
<textarea name="message" rows="5" cols="40">
</textarea><br /><br />Comments:<br />
<textarea name="comments" rows="5" cols="40">
</textarea><br /><br /><input type="submit" value="Please Submit" />
<input type="reset" value="Reset Form" />
</div>
</form><?php
if (
!empty ($_POST['email']) &&
!empty ($_POST['name']) &&
!empty ($_POST['message'])
)//if "email", "name" or "message" is not filled out, display the form
{
echo "<div class=\"cen\">" . "Please enter your info...";
}else
{//send mail
mail("robazar@earthlink.net", $subject,
$message, $headers);
echo "<div class=\"cen\">" . "Thank you for sending your info.<br />You will be contacted soon." ."</div>";
}?>
</body>
</html>

You missed something
!empty ($_POST['email']) &&
see the exclamation mark, which means "not".
The same can be applied to isset.
if (isset ($var))
/* If the variable $var is defined */if (!isset ($var))
/* If the variable $var isn't defined */if (empty($var))
/* If the variable $var is empty or not defined */if (!empty ($var))
/* If the variable $var exists and not empty */
---
Fubar

Hmmm... Did you change anything on your post? I think I saw that you're missing the exclamation mark. But now it's not 0_0
Nevermind. I think my eyes fools me.
Take a look below:
http://broadband.or.id/prolindo/tes...
http://broadband.or.id/prolindo/tes...
The scheme is a bit different, but maybe then you can understands it more.
---
Fubar

After a good night's sleep, the fog on the brain cleared and I was presented with the solution to my problem...
I was wanting to test to see if the 3 fields were populated (!empty) AND if they were, to send the email. Period.
I was actually testing to see if the fields were empty (empty), and if they were, to display a message requiring them to be filled, then when they were (populated, all three), to submit the mail...
(here comes the fog again!)
This morning, I test to see if the fields are NOT EMPTY, and send the mail. Period. Like:if (
!empty ($_POST['email']) &&
!empty ($_POST['name']) &&
!empty ($_POST['message'])
)
//send mail
{
mail("robazar@earthlink.net", $subject,
$message, $headers);
echo "<div class=\"cen\">" . "Thank you for sending your info.<br />You will be contacted soon." . "</div>";
}else
echo "<div class=\"cen\">" . "Please fill in all fields on the form." . "</div>";
And this works pretty well. But I gotta be honest. I'd rather not have the "Please fill in..." message appear until after the form (not populated) is submitted. Now, when the page launches, the fields are (obviously) not populated, and the "Please..) message is echoed. BUT, the links you sent will fix what ails me. Didn't know the "$dontshowform" trick yet, or the isset-submit.
Thanks for your help w/ my education so far. I appreciate the time you've put in to bring a newbie along!
Where I'm at this morning:http://www.perfectionconstruction.c...

So, I stole some of your code (thx) and now I get the form, if I submit empty, no echo, if I submit full, no echo, no email. I think I might be nesting wrong or...? Page:
http://www.perfectionconstruction.c...
Code: (all between body tags)<?php
$email = $_REQUEST['email'] ;
$subject = stripslashes($_POST['subject']);
$message = "Name: " . stripslashes($_POST['name']) . "<br />";
$message .= "Email: " . stripslashes($_POST['email']) . "<br />";
$message .= "Message: " . stripslashes($_POST['message']) . "<br />";
$message .= "Comments: " . stripslashes($_POST['comments']) . "<br />";// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email";// IF Submit
if (isset ($_POST['Submit']))
{// Do email process and show thank you page
// But first, check the fields
if (
!empty ($_POST['email']) &&
!empty ($_POST['name']) &&
!empty ($_POST['message'])
)
{// Fields OK
/* Do mailing functions here */mail("robazar@earthlink.net", $subject,
$message, $headers);// Show thankyou page
echo 'Thank you!</p>' . "\n";
// Dont show the form
$dontshowform = true;
}
else
{
// Fields not OK, show notice
echo 'Both fields must be filled!</p>' . "\n";
}
}if (!isset ($dontshowform)) { ?>
<h1>Personal Information</h1>
<form method="post" action="mail7.php">
<div>
Name: <input name="name" type="text" /><br /><br />Email: <input name="email" type="text" /><br /><br />
Subject: <input name="subject" type="text" /><br /><br />
Message:<br />
<textarea name="message" rows="5" cols="40">
</textarea><br /><br />Comments:<br />
<textarea name="comments" rows="5" cols="40">
</textarea><br /><br /><input type="submit" value="Please Submit" />
<input type="reset" value="Reset Form" />
</div>
</form><?php } ?>

Fugitaboutit!
Found the prob. Wicked simple, usually the culprit. My Submit had no name value. 'nuf sed...

FWIW:
(since I know there are exactly 23 other newbies out there trying to pull off exactly the same thing I'm trying...) And I know I'm not telling you anything you didn't already know, but you gotta do double quotes and concatenate the inline style in the echo or she don't go:// Show thankyou page
echo "<div class=\"cen\">" . "Thank you! Your Info has been submitted." . "</div>" . "\n";Who knew? :|

Enclosing with double quotes means PHP will try to process any PHP-processable inside.
And using double quotes means you must not use it again inside, unless to "close" the statement. If you must use it, escaping (with backslash) is required.
Same applies to single quote.
Let's say:
<?php
$var = 'bar\'s';
// Outputs foo bar's
echo "foo $var";// Outputs foo $var
echo 'foo $var';// Outputs foo bar's
echo 'foo ' . $var;?>
So in general, single quote is better because PHP won't try to test and proses every statement, which we probably meant the statement just to be echoed only.
Of course there're cases where double quotes are better, let's say:
<?php
$d = '14';
$m = 'November';
$y = '2007';// This is (IMO) better:
echo "$d-$m-$y";// ...than this one:
echo $d . '-' . $m . '-' . $y;
?>
----
So If I were about to echo DIVs, I'll do this:<?php
// Show thankyou page
echo '<div class="cen">Thank you! Your Info has been submitted.</div>' . "\n";
// The "\n" is a PHP-processable atatement, which telling PHP to output a linebreak, so the code is more readable when people "view source" the HTML result.
?>---
Fubar

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |