How do I stop a form submitting is there are blanks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnitemare
Forum Newbie
Posts: 12
Joined: Sat Mar 28, 2009 1:14 pm

How do I stop a form submitting is there are blanks

Post 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;
Last edited by Benjamin on Fri Apr 24, 2009 7:23 pm, edited 1 time in total.
Reason: Changed code tag type from text to php
kaiser0427
Forum Newbie
Posts: 9
Joined: Thu Apr 23, 2009 3:38 pm

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

Post by kaiser0427 »

Code: Select all

    if($_POST['field_name'] == "" ) {
        //return error message
}
 
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

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

Post 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)){}
Post Reply