LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-08-2012, 12:53 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: India
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,177

Rep: Reputation: 48
When form field is left empty, I want the SAME PAGE but with a red warning (PHP)


...on my site, if a user submits a form with entry fields blank, I want the user to get the same page, but with some warning text - "You have left this field blank" - how is this architected in PHP?


Thanks.
 
Old 02-08-2012, 01:48 AM   #2
resetreset
Senior Member
 
Registered: Mar 2008
Location: India
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,177

Original Poster
Rep: Reputation: 48
Hey Sorry,
I just realised that I'd posted about this before.

Well anyway, if anyone still has something fresh to offer, I'd love to listen!


Thanks.
 
Old 02-08-2012, 05:45 AM   #3
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackware64 13.37-multilib
Posts: 480

Rep: Reputation: 71
I think this is typically done with a javascript that validates the form fields and submits it to the next page if the validation passes.
 
Old 02-08-2012, 06:21 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,554
Blog Entries: 3

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
I've used a construct similar to this one.

The form page itself, say form.php:
PHP Code:
<?PHP

    
/* Error array, keyed on input field name. */
    
$errors = array();

    
/*
     * ... form validation ...
    */
    
if (!preg_match(/[0-9A-Za-z]@.+.[0-9A-Za-z]+$/, @$_POST['email']))
        
$errors['email'] = 'You must supply a valid e-mail address.';

    if (
count($errors) > 0)
        require_once(
'form.fill.inc');
    else
        require_once(
'form.save.inc');
?>
The form.fill.inc is the actual page that produces the form itself. It will have constructs similar to say
PHP Code:
  <td class="label">E-mail address:</td>
  <td class="input"><?PHP
    
echo '<input type="text" name="email" value="',
          
htmlentities(@$_POST['email'], ENT_COMPAT'UTF-8'),
         
'" maxlength="128">';
    if (
array_key_exists('email'$errors))
        echo 
'<span class="error">',
              
htmlentities($errors['email'], ENT_COMPAT'UTF-8'),
             
'</span>';
    
?></td>
Obviously you should use a helper function for each type of input field (one-line text input, multiline text input, table of radio buttons) instead of open-coding it like I did above. Just as an example, consider
PHP Code:
<?PHP
function textinput($name$class ''$maxlength 0) {
    global 
$errors;

    echo 
'<input type="text" name="'$name;
    if (
strlen($class) > 0)
        echo 
'" class="'$class;
    echo 
'" value="'htmlentities(@$_POST[$name], ENT_COMPAT'UTF-8');
    if (
$maxlength 0)
        echo 
'" maxlength="'$maxlength;
    if (
array_key_exists($name$errors))
        echo 
'"><span class="error">',
              
htmlentities($errors[$name], ENT_COMPAT'UTF-8'),
             
'</span>';
    else
        echo 
'">';
}
?>

  <td class="label">E-mail address:</td>
  <td class="input"><?PHP textinput('email'); ?></td>
When the form is filled properly, the actual processing is done by the form.save.inc page.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form field validation using php esteeven Programming 2 09-10-2009 02:15 PM
php question, how do I get a return from a field within a field? cherrington Programming 11 04-29-2009 01:27 AM
Default value in a php variable if a form-field is empty? MheAd Programming 2 12-19-2008 06:21 AM
How to write to a file from a form with perl, and not leave the form page. OldGaf Programming 3 11-12-2008 07:53 PM
lsmod:used by field is empty? Sailaja Reddy Red Hat 0 08-24-2004 09:30 AM


All times are GMT -5. The time now is 06:09 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration