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

Dani AI

Generated

Quick practical plan (client + server) that fills gaps in the thread.

As said, use both client-side and server-side validation. Start by adding HTML5 required/type attributes so browsers will block empty fields and give the right keyboards/UX (mobile). This is quick and visible to users. Then add a small unobtrusive submit check (Constraint Validation API) and a simple server-side routine that enforces the same rules and sanitizes input before sending mail.

Example (HTML): add required and proper type where appropriate:

<input name="forename" required>
<input type="email" name="email" required>
<input type="tel" name="telephone" pattern="[0-9 +()-]{6,}" required>

Example (light JS fallback): prevent submit and focus first invalid field:

document.getElementById('application').addEventListener('submit', function(e){
  if (!this.checkValidity()) {
    e.preventDefault();
    this.querySelector(':invalid').focus();
  }
});

Server-side checklist (PHP): validate required fields, then validate formats (use filter_var for email), trim/sanitize all values, and only if validation passes send the mail. A minimal pattern:

$required = ['forename','surname','email', ...];
$errors = [];
foreach ($required as $f) {
  if (empty(trim($_POST[$f] ?? ''))) $errors[$f] = 'Required';
}
if (!filter_var($_POST['email'] ?? '', FILTER_VALIDATE_EMAIL)) $errors['email']='Invalid email';

Security and practical fixes:

  • Don’t trust the client: always run the server checks even if HTML5/JS exist. (Echoes ’s point about server validation.)
  • Never use raw user input for the From: header. Use a fixed domain address (e.g. no-reply@yourdomain) and put the validated user address in Reply-To after stripping CR/LF to avoid header injection.
  • Fix obvious typos/mismatches that will break validation: your form has name="adress2" but PHP expects address2, and the form id="applicatipn" will break JS that looks for application. Also check variable name typos like $telephome.
  • For production, consider a mail library (PHPMailer) to avoid header issues and to send properly encoded HTML mail.

Suggested workflow: add HTML5 required → add the small JS snippet → implement the server-side checks above → test with JS disabled and with malformed inputs. This sequence gives immediate UX benefits and robust protection on submit.

Recommended Answers

All 5 Replies

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.