I have the following form that I need the fields to be mandatory before submission:

form name="application" id="applicatipn" method="post" action="send_mail.php" >
<h2 class="title">form</h2>
<p class="sub-text">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
</p>
<table width="620px">
<tr class="bg-grey">
<td>
<h3 >Personal Details</h3>
</td>
</tr>
</table>
<table width="600px" class="bg-light-grey" style=" margin-left: 10px">
<tr>
 <td valign="top">
  <label>Title</label>
 <br />
  <input type="text" name="title" size="5" class="auto-width" />
  <br />
  <br />
 </td>
</tr>

<tr class="no-margin-bottom">
 <td valign="top">
  <label>Forenames</label>
 <br />
  <input type="text" class="no-margin-bottom" name="forename" size="30" />
 </td>
</tr>
<tr>
 <td valign="top">
  <label>Surname</label>
 <br />
  <input type="text" name="surname" size="30" />
  <br />
  <br />
 </td>

</tr>
<tr>
 <td valign="top">
  <label>Address</label>
 <br />
  <input type="text" name="address1" size="30" />
  <br />
  <input type="text" name="adress2" size="30" />
 </td>
</tr>



<tr>
 <td valign="top">
  <label>City</label>
 <br />
  <input type="text" name="city" size="30" class="auto-width" />

 </td>
</tr>
<tr>
 <td valign="top">
  <label>Postcode</label>
 <br />
  <input type="text" name="postcode" size="10" class="auto-width" />
  <br />
  <br />
 </td>

</tr>
<tr>
 <td valign="top">
  <label>Telephone No</label>
 <br />
  <input type="text" name="telephone" size="30" placeholder="    +      4      4        " />
  <br />
  <br />
 </td>
</tr>

<tr>
 <td valign="top">
  <label>National Insurance Number *</label>
 <br />
  <input type="text" name="insurance_num" size="30" class="auto-width" />
 </td>
</tr>

<tr>
 <td valign="top">
  <label>Date Of Birth</label>
 <br />
  <input type="text" name="dob" size="12" class="auto-width" placeholder="  D D    M M   Y Y    "/>
  <br />
  <br />

 </td>
</tr>
<tr>
 <td valign="top">
  <label>Email Address *</label>
 <br />
  <input type="text" name="email" size="30" style="width:55%" />
  <br />
  <br />
 </td>
</tr>
</table>

<table width="620px">
<tr class="bg-grey">
<td>
<h3 >Loan Information</h3>
</td>
</tr>
</table>

<table width="600px" class="bg-light-grey" style="margin-left: 10px">
<tr>
 <td valign="top">
 <br />

  <label>XXXXXXXXXXXXXXXXXXXX</label>
 <br />
 <span> @ </span> <input type="text" name="loan_amount" size="5" class="auto-width" />
  <br />
  <br />
 </td>
</tr>
</table>

<table width="620px">
<tr class="bg-grey">
<td>
<h3 >Terms &amp; Conditions</h3>
</td>
</tr>
</table>

<table width="600px" style="margin-left: 10px">
<tr>
 <td valign="top" style="border: 1px solid">
  <p style="font-size: 10px; margin: 5px 5px 5px 5px; font-weight: 500; padding:10px 0px">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p>
</td>
</tr>
<tr>
<td>
<p style="text-align: center; font-size: 9px;font-weight:bold">XXXXXXXXXXXXXXXXXXXXXXXXXX</p>
 </td>
</tr>
</table>

<table width="620px">
<tr class="bg-grey">
<td>
<h3 >Customers Signature</h3>
</td>
</tr>
</table>

<table width="600px" style="background-color: #c1c1c1; margin-left: 10px;border: 1px solid">
<tr>
 <td valign="top" >
 <br />
 <input type="text" class="signature" name="signature" size="15" placeholder="Signature" style="border: 1px solid"/>
 </td>
</tr>
</table>

<table width="620px">
<tr class="bg-grey">
<td style="text-align:center;">
<input type="submit" name="submit" id="submit">
</td>
</tr>
</table>
</div>
</form>

The PHP:

<?php
if(isset($_POST['submit']))
{
$title=$_POST['title'];
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$city=$_POST['city'];
$postcode=$_POST['postcode'];
$telephome=$_POST['telephone'];
$insurance_num=$_POST['insurance_num'];
$dob=$_POST['dob'];
$email_id=$_POST['email'];
$loan_amount=$_POST['loan_amount'];
$signature=$_POST['signature'];
$to = 'XXXXXXXXXXXXXXXXXXXX';

$subject = 'From Website';

$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Title:</strong> </td><td>" . strip_tags($_POST['title']) . "</td></tr>";
$message .= "<tr><td><strong>Forename:</strong> </td><td>" . strip_tags($_POST['forename']) . "</td></tr>";
$message .= "<tr><td><strong>Surname:</strong> </td><td>" . strip_tags($_POST['surname']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" . strip_tags($_POST['address1']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" . strip_tags($_POST['address2']) . "</td></tr>";
$message .= "<tr><td><strong>City:</strong> </td><td>" . strip_tags($_POST['city']) . "</td></tr>";
$message .= "<tr><td><strong>Post code:</strong> </td><td>" . strip_tags($_POST['postcode']) . "</td></tr>";
$message .= "<tr><td><strong>Telephone:</strong> </td><td>" . strip_tags($_POST['telephone']) . "</td></tr>";
$message .= "<tr><td><strong>Insurance Numebr:</strong> </td><td>" . strip_tags($_POST['insurance_num']) . "</td></tr>";
$message .= "<tr><td><strong>Date of birth:</strong> </td><td>" . strip_tags($_POST['dob']) . "</td></tr>";
$message .= "<tr><td><strong>Email id:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Loan Amount:</strong> </td><td>" . strip_tags($_POST['loan_amount']) . "</td></tr>";
$message .= "<tr><td><strong>Signature:</strong> </td><td>" . $_POST['signature'] . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
if(mail($to, $subject, $message, $headers))
{
echo '<script> alert("Submit successfully")</script>';
echo '<script> location.href="index.html" </script>';
}
else
{
echo '<script> alert("Mail not send! Please again try!")</script>';
echo '<script> location.href="apply.html"</script>';
}
}
else
{
echo '<script> location.href="xxx.html"</script>';
}
?>

Thanks in advance

You should consider client and server side validation. The reason is if you simply use client side validation, the end-user can easily bypass that validation process by disabling javascript. Server side validation is needed regardless to ensure the data received is validated prior to further processing.

I dont see any attempt in your code regarding validation. What have you tried? what are you looking for as far as help?

Hi and thanks for your reply,

To be honest my specialist field is SEO and marketing but as i've dabbled in this before I've offered to help a friend out with this.

I managed to knock the form up easy enough but this is the extent of my knowledge to be honest, I have no idea where to start and as its a favor for a friend I can't exactly outsource it so looking for help sorting it.

Id be happy to offer my SEO and Marketing services to an equivilant value to anyone who can make the fields required for me :)

Theo

hello theo,

I am not the person that will be able to exchange services for work; in any event, i'm not a php developer. I work with asp.net.

however, on the client side, this can be done from scratch, or there are various plug-ins out there that can help with the validation. For example.. http://jqueryvalidation.org/

Server side... someone else is going to have to chime in for php guidance.

Maybe someone else will take you up on that offer. this site does have a business exchange section that you may consider as an option for getting further assistance if this thread doesnt produce the assistance you are requesting.

The way you can do this fast and easy is something like this

before the submit button validation, define all the errors like this

<?php

    $title_error = '';
    $forename_error = '';
    $surname_error = '';
    ## do the same for all of the items needing validation

    ## check if the submit button is set

    if(isset($_POST['submit'])){

        if(!empty($_POST['title']){
            $title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
        }

        else{

            $title_error = 'Title is required';

           }
     }      

You do the same for the remaining and on the html form you do like this

 <input type="text" name="title">
 <span><?php echo $title_error;?></span>

You can style the <span> with css. It is all up to you.

There is another method of form validation, but it is more complex and maybe you guys can take a look at it. It is called filter_input_array(), you can read on how this function is used in form validation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.