We help IT Professionals succeed at work.

Suppress comment field in submitted PHP form if it's blank

kmurphychi asked
Last Modified: 2012-05-06
I have a form that people fill out to send a product to their friend.  I would like to be able to suppress the comment field being included if it is left blank by the user.   Thanks for any help!  Below is the code:

if(isset($_POST["_task"]) && $_POST["_task"]=="SEND"){
$sender = $_POST['sender'];
$comment = $_POST['comment'];

$subject = "subject";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: email' . "\r\n";

for($i = 1; $i <=4; $i++) {
$r_email = "email_".$i;
$email = $_POST[$r_email];

if($r_email != "email_") {

$message='<p style="width:450px; font-family:arial; font-size:18px;color:#717783;">These photos are being sent to you at the request of '.$sender.'</p><p style="width:450px; font-family:arial; font-size:18px;color:#717783;">'.$comment.'</p><table style="width:450px; font-family:arial; font-size:18px;color:#717783;" border="0" width="480" cellspacing="0" cellpadding="0"><tr>';
$sql = mysql_query("select p.productid,pd.productname,pd.price,pd.description,pd.thumb from products p , products_description pd where pd.productid = p.productid and p.productid = $id");
$row = mysql_fetch_row($sql);
$productname= str_replace(" Collection", "", $productname);

$menu_id      = $_GET['menulevel'];
$category_id= $_GET['cat_id'];
$productid       = $_GET['productid'];
$producturl      = "

$message .= '<tr><td valign="top" align="center"><p style="font-family:arial; font-size:18px;color:#717783;"><a href="'.$producturl.'"><img src="/product-images/thumb/'.$thumb.'"  border="0" /></a><br><a style="color: #696969;font-family:arial; font-size:18px;" href="'.$producturl.'">Click Here</a> to view product details</p><div style="width:450px; font-family:arial; font-size:18px;color:#717783;">'.$productname.'</div><div style="width:450px; font-family:arial; font-size:18px;color:#717783;">'.$descriptions.'</div></td></tr></table>';}
$message .= '<p style="width:550px; font-family:arial; font-size:18px;color:#717783;">For more looks and options, please visit <a style="color: #696969;font-family:arial; font-size:18px;" href=".</p><br><p style="width:450px; font-family:arial; font-size:18px;color:#717783;"><br><br> Phone:(312) 733-9700</p>';
mail($email, $subject, $message, $headers);
Watch Question

Chris SandriniSenior System Engineer
Top Expert 2012

Not sure if I understood you right. You are looking for this?

if (isset($_POST['comment') && $_POST['comment'] != "")
	// Add Comment
} else
	// Don't add comment

Open in new window

This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.