Jump to content

Recommended Posts

Hello All:

 

I have a HTML form set up that works fine but if there is nothing entered in any of the fields I get a blank screen. I wanted to set it up so that it comes back with saying what is missing so I wrote out

 

if (empty($_POST["First_Name"])) {
	$First_Name = FALSE;
	$message .= 'Please Enter Your First Name';
}else{
$data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"]));
}

 

However, when I fill out the form and leave the first name blank, it still goes ahead and dumps my info in my database.

 

Any thoughts on what I might be doing wrong here?

 

Thanks,

 

Paul

 

Try

 

if ($_POST["First_Name"] == "" || $_POST["First_Name"] == NULL) {
$First_Name = FALSE;
	$message .= 'Please Enter Your First Name';
}else{
$data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"]));
}

 

Let me know if this works.

<?php
if (empty($_POST["First_Name"])) {
  $First_Name = FALSE;
  $message .= 'Please Enter Your First Name';
}else{
  $data .= "&First_Name=" . urlencode(strip_tags($_POST["First_Name"]));
}
?>

 

What are you doing after this?  If your code is set to just go ahead and process the form afterwards it doesn't matter what checks you perform, right?

 

The correct logic for processing a form is:

IF validate() THEN
  process()
ELSE
  show()
END

 

I'm guessing that your code expresses this logic:

IF submitted() THEN
  process()
ELSE
  show()
END

 

(edit) Also, never jump to conclusions while debugging.  Your topic title states if empty isn't working.  How do you know that?  Which test did you specifically run to make sure that empty() is failing?  Perhaps empty() is working perfectly and your code is failing elsewhere.

If $First_Name is blank after all that, try this:

 

echo ".{$_POST['First_Name']}.";

 

If there's a space, then $_POST['First_Name'] is a space (which is a value and will not eval as empty).

 

Something is causing empty() and isset() to eval to true, which plainly indicates it does, definately have a value.

 

You can also var_dump the variable and see what it shows.

 

Proves " " is a value:

 

<?php
$string = " ";

if(isset($string)) {
print '$string has a value'."\n\n<br /><br />";
}

if(!empty($string)) {
print '$string has a value'."\n\n<br /><br />";
}

echo ".$string."; // Output: . .

?>

(edit) Also, never jump to conclusions while debugging.  Your topic title states if empty isn't working.  How do you know that?  Which test did you specifically run to make sure that empty() is failing?  Perhaps empty() is working perfectly and your code is failing elsewhere.

 

Yes, you are correct. I did not test it properly to see if that was the case. Just newbie frustration I guess.

 

:'(

Yes, but then how do I tell it to stop if $_POST["First_Name"] is not valid?

 

that is where I get lost..

 

You don't tell it to stop.  You want it to continue.  What you don't want it to do is process.  Here is a simple form class.

 

<?php
class form {
  // array of error messages
  var $errs = Array();

  // constructor
  function form(){
    // This would be a good place to check if magic quotes is on and if it is,
    // call stripslashes recursively on all $_POST elements
  }

  // process - this function processes the form; i.e. does DB stuff, whatever
  function process(){
    // insert into db and display success message
  }

  // show - this function shows the form
  function show(){
    $errs = '';
    if(!empty($this->errs)){
      // We have errors
      $errs = implode('</li><li>', $this->errs);
      $errs = "
        <div class=\"errors\">
          <p>
            The following errors were encountered while processing your request:
          </p>
          <ul>
            <li>{$errs}</li>
          </ul>
        </div>
      ";
    }

    // prepare the default values
    $fields = Array( 'fname', 'lname' );
    $defaults = Array();
    foreach($fields as $f){
      $defaults[$f] = isset($_POST[$f]) ? _$POST[$f] : '';
    }

    // Display the form
    echo <<<HTML
  <form method="post">
    {$errs}
    <fieldset>
      <legend>Name</legend>
      <label for="fname">First:</label>
      <input type="text" id="fname" name="fname" value="{$defaults['fname']}" />
      <label for="lname">Last:</label>
      <input type="text" id="lname" name="lname" value="{$defaults['lname']}" />
      <input type="submit" name="submit" value="Submit" />
    </fieldset>
  </form>
HTML;
  }

  // validate - this function validates the form; returns false if invalid
  function validate(){
    $has_errors = false;               // Assume it has no errors

    // If $_POST is empty, form has not been submitted, so we want to return
    // false (as if the form *DID* have errors)
    if(empty($_POST)){
      return false;
    }

    // Now we can validate

    // First name field - required
    if(empty($_POST['fname'])){
      $has_errors = true;
      $this->errs[] = "First name is required.";
    }

    // last name - required
    if(empty($_POST['lname'])){
      $has_errors = true;
      $this->errs[] = "Last name is required.";
    }

    // name fields are alpha only
    if(!ctype_alpha($_POST['fname']) ||
       !ctype_alpha($_POST['lname'])){
      $has_errors = true;
      $this->errs[] = "Name fields are alphabetic only.";
    }

    // and so on for other fields

    // we return, using reverse logic
    return !$has_errors;
  }

  // do - this function runs the form
  function do(){
    // run the form
    if($this->validate()){
      $this->process();
    }else{
      $this->show();
    }
  }

}
?>

 

Example code to run the form

<?php
  $form = new form();
  $form->do();
?>

 

Take a look at that, absorb it, maybe even run it on your own server and see how it works.  It's not perfect code, or even how I do things, but it should give you some ideas.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.