If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Validation using php

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-31-10, 03:37
don_log don_log is offline
Registered User
 
Join Date: Jun 2008
Location: pakistan
Posts: 109
Validation using php

i want to do form validation using php when field left empty and submit a form
if field is empty then show fill in the name on this page like this
Attached Thumbnails
Validation using php-firm.jpg  
Reply With Quote
  #2 (permalink)  
Old 06-01-10, 06:04
ProphetX ProphetX is offline
Registered User
 
Join Date: Jun 2010
Location: New Zealand
Posts: 15
The most basic approach you can use is manually validating each field and returning an error message if validation fails:

PHP Code:
<?php
if (isset($_POST['submit']))
{
    
$success TRUE;

    
//Put each $_POST value into its own array
    
foreach ($_POST as $key => $val)
    {
        $
$key $val;
    }

    
//Form validation starts here
    
if (empty($message))
    {
        
$success FALSE;
        
$message_error 'The message field cannot be empty.';
    }

    if (
$success == TRUEredirect('/success_page.php');
}
?>

<form action="this_page.php">
<label for="message">Name:</label> <input type="text" name="message" id="message" value="<?=$message?>" />
<?php if (!empty($message_error)) echo $message_error?>

<br />
<input type="submit" name="submit" value="Send Message" />
</form>
There are loads of nice validation libraries available that will take care of the hard work for you. CodeIgniter is an MVC framework that has a great validation library - definitely worth checking out.
Reply With Quote
  #3 (permalink)  
Old 06-01-10, 06:45
saviola saviola is offline
Registered User
 
Join Date: Jun 2010
Posts: 6
If you don't want to do verification of all form fields, you just can register an array and its keys have to be composite from require fields name like keys ...
PHP Code:
<?php

$reauireFields 
= array('fieldOne''fieldTwo');
if (isset(
$_POST['submit']))
{
    
$success TRUE;

    foreach (
$reauireFields as $key => $val)
    {
        if(empty(
$_POST[$key])) {
            
$success FALSE;
            
$message_error[$key] = 'The message field cannot be empty.';
        }
    }

    if (
$success == TRUEredirect('/success_page.php');
}
?>

<form action="this_page.php">
    <label for="fieldOne">fieldOneName:</label> 
    <input type="text" name="fieldOne" id="fieldOne" value="<?=$_POST['fieldOne']?>" />
<?php if (!empty($message_error['fieldOne'])) echo $message_error['fieldOne']; ?>

<br />
<input type="submit" name="submit" value="Send Message" />
</form>
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On