Page 1 of 1

How do I stop a form submitting is there are blanks

Posted: Fri Apr 24, 2009 6:10 pm
by phpnitemare
Hi,

i`m trying to stop a form submitting if there are blank text boxes and display error messages. have tried various solutions from the internet but none are working. Any suggestions? With the solution below I just get undefined checkRequiredPost.

Thanks

Code: Select all

<?php
 
$requiredField = array('fname', 'lname');
                          
if (checkRequiredPost($requiredField)) {
extract($_POST);
 
 
 
?>
 
 
<script language="JavaScript" type="text/javascript" src="utils/checkout.js"></script>
 
 
<table width="550" border="0" align="center" cellpadding="10" cellspacing="0">
    <tr> 
        <td> Enter Shipping And Payment Information </td>
    </tr>
</table>  
<form action="customerconfirmation.php" method="post"> 
<table width="550" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
        <tr class="entryTableHeader"> 
            <td colspan="2">Shipping Information</td>
        </tr>
                <tr> 
            <td width="150" class="label">First Name</td>
            <td class="content"><input name="fname" type="text" class="box" id="fname" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Last Name</td>
            <td class="content"><input name="lname" type="text" class="box" id="lname" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Address1</td>
            <td class="content"><input name="address1" type="text" class="box" id="address1" size="50" maxlength="100"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Address2</td>
            <td class="content"><input name="address2" type="text" class="box" id="address2" size="50" maxlength="100"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Post code</td>
            <td class="content"><input name="postcode" type="text" class="box" id="postcode" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Email</td>
            <td class="content"><input name="email" type="text" class="box" id="email" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Phone</td>
            <td class="content"><input name="phone" type="text" class="box" id="phone" size="30" maxlength="32"></td>
        </tr
        <tr> 
            <td width="150" class="label">Credit card no.</td>
            <td class="content"><input name="cardno" type="text" class="box" id="cardno" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Expiry date</td>
            <td class="content"><input name="expiry" type="text" class="box" id="expiry" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Card type</td>
            <td class="content"><input name="cardtype" type="text" class="box" id="cardtype" size="50" maxlength="100"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Security Code</td>
            <td class="content"><input name="seccode" type="text" class="box" id="seccode" size="3" maxlength="3"></td>
        </tr>
 
 
 
<input type="submit" />
        </form>
 
 
<?php
      exit;

Re: How do I stop a form submitting is there are blanks

Posted: Fri Apr 24, 2009 6:24 pm
by kaiser0427

Code: Select all

    if($_POST['field_name'] == "" ) {
        //return error message
}
 

Re: How do I stop a form submitting is there are blanks

Posted: Fri Apr 24, 2009 6:24 pm
by jazz090
when u use javascript to validate data, u need to have a backup validator built in php iteslef in case the user has js turned off on their browser,
as far as js goes, just test it with if (field == ""){} and in php test it with if (empty($field)){}