How to make form fields REQUIRED in php

I know absolutely nothing about php. I have an html file that incorporates a contact form (see below for a portion of the form) and I need to have the Name, E-Mail, and Phone fields as REQUIRED so that these fields are not left blank:

[php]

	<tr>
		<td width="250"><font FACE="Verdana" SIZE="1" COLOR="#000000"><strong>Name:</strong></font> <font FACE="Verdana" SIZE="2" COLOR="#654b34"><strong>*</strong></font></td>

		<td><input type="text" size="30" name="Name" /></td>
	</tr>

	<tr>
		<td width="250"><font FACE="Verdana" SIZE="1" COLOR="#000000"><strong>E-Mail:</strong></font> <font FACE="Verdana" SIZE="2" COLOR="#654b34"><strong>*</strong></font></td>
		<td><input type="text" size="30" name="e-mail" /></td>
	</tr>

	<tr>

		<td width="250"><font FACE="Verdana" SIZE="1" COLOR="#000000"><strong>Phone:</strong></font> <font FACE="Verdana" SIZE="2" COLOR="#654b34"><strong>*</strong></font></td>
		<td><input type="text" size="30" name="Phone" /></td>
	</tr>
	<tr>

The actual form mechanics are run by a php file provided by the website hosting company and the info is emailed to the website owner. I contacted the hosting company to see if they could assist me in, but they indicated they do not provide such assistance. Below is the php file that runs the form:

<?php $request_method = $_SERVER["REQUEST_METHOD"]; if($request_method == "GET"){ $query_vars = $_GET; } elseif ($request_method == "POST"){ $query_vars = $_POST; } reset($query_vars); $t = date("U"); $file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t; $fp = fopen($file,"w"); while (list ($key, $val) = each ($query_vars)) { fputs($fp,"\n"); fputs($fp,"$val\n"); fputs($fp,"\n"); if ($key == "redirect") { $landing_page = $val;} } fclose($fp); if ($landing_page != ""){ header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page"); } else { header("Location: http://".$_SERVER["HTTP_HOST"]."/"); } ?>[/php]

The website is on a Linux platform. I went to http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_required and tried to see if I could cut and paste some of that material into both my html form file and the php file, but it did not work insofar as when I tested it, it still allowed me to submit the form even though I thought that these fields were now REQUIRED and that the form would not allow me to submit it without the Name, E-Mail, and Phone fields filled in. I am at my wits end in trying to make this work. PLEASE HELP!

PHP works server side so in order for PHP to validate it you will have to submit the form.

You are probably thinking about having javascript validate the form (you should actually do both). Look into jquery validator, it’s an easy way to validate forms :slight_smile:

For the server side you should just check if the fields are what you expect.

[php]if (!filter_var($_POST[‘email’], FILTER_VALIDATE_EMAIL)) {
// error
}[/php]

HTML5 allows the inclusion of ‘required’ within the elements
Username:

check it out here: HTML5 W3

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service