Jump to content

Ignore empty fields in form, how?


pcoxygen

Recommended Posts

I know absolutely nothing about PHP script.  But I do have a little problem with what I have done in some very easy code.

 

I'm creating an HTML user Form that when Submitted, it gets posted to a PHP page to print on screen what the user typed in the form fields.

 

PROBLEM:

With my existing PHP script, it writes a blank line where no data is in the HTML field.  I don't want that.  I want the results to skip or ignore the HTML field that was left blank by the user.

 

The HTML form page (form.html) is this:

 

<html>
<body>
<form action="label_print.php" method="get">
   Your Name:* <input name="name" type="text" /> <br />
   Company: <input name="company" type="text" /> <br />
   Address: * <input name="address1" type="text" /> <br />
   Address: <input name="address2" type="text" /> <br />
   City:* <input name="city" type="text" /> <br /> State:* <input name="state" type="text" /> <br />
   Zip Code:* <input name="zip" type="text" /> <br />
   <br /><br />
   <input type="submit" value="Make Label Now" />
</form>
</body>
</html>

 

The PHP to print the Form fields (label_print.php) script is this:

<html>
<body>
<?php
/* 
  * This will print whatever the user put into the form on the form.html page.
  */

$name = $_REQUEST['name'];
$company = $_REQUEST['company'];
$address1 = $_REQUEST['address1'];
$address2 = $_REQUEST['address2'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];

echo "From:<br />". $name ."<br />". $company ."<br />". $address1 ."<br />". $address2 ."<br />". $city .", ". $state ." ". $zip ."";

?>
</body>
</html>

 

A working example of the above can be found here: http://www.certifiedlaptoprepair.com/shipping_label/form.html

 

So can anyone offer any advice what I need to add to the PHP to make it ignore blank field in the Form?  Any other suggestion are also appreciated (if I have typed some wrong PHP code, or how to make it more efficient).

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/172657-ignore-empty-fields-in-form-how/
Share on other sites

Ok, I thought I found the answer somewhere on the web.  But what MasterACE14 posted is better.  This is what I found:

 

Removing the whole PHP line starting with echo (post above).

 

if (!empty($name)) {
print "$name<br>";
}

 

However, what you have provided is really what I want because it has the "else", which alerts the user if there is a required field.  Awesome!  Thank you for your time and skill.  I wish I knew PHP like you guys.  It's such a valuable language.

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.