Jump to content


Simplest way of doing this

  • Please log in to reply
1 reply to this topic

#1 play_

  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 June 2006 - 01:50 AM

I wrote a function that checks to make sure fields are not empty.

Case: i have 3 input fields. im making sure they are not empty. function:
$message = null;
$num = 0;
function validate($field, $error_message) {
    global $message;
    global $num;    
    if(empty($_POST[$field])) {
        $message .= $error_message. '<br />';
    } else {
        #print "<b>$field</b>";
        $field = $_POST[$field];

Then i call the function 3 times:
        validate('name', 'Please enter your name.');
    validate('email', 'Please enter your e-mail.');
    validate('text', 'Please enter a message.');

when the user clicks submit, for each field that is NOT empty, $num is incremented by 1 ($num++).
Since i have 3 fields, i check with this:

    if($num == 3) {
        // email me the form.

So that's how i have it. But i'd like to make it more extensible/simpler. So does anyone know anyway to improve this?
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 litebearer

  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 01 June 2006 - 02:06 AM

Unfortunately, empty() will accept spaces as being valid,

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users