Jump to content

New here and New to PHP


nepzap2

Recommended Posts

I have started my journey learning about Databases and PHP. I'm a Newbie who has tons of web design experience but never had any real Database experience. I love what I'm learning, I truly believe this is the way to go. Anyways, I had a little question... I'm currently working on validating a simple form and I've come up with the code below. I want to be able to display simultaneous error reporting in a form if more than one field are left blank. I got it to report individual fields if they are left blank but not multiple. Any help is appreciated.

 

Thanks,

 

Begin of code

 

 

<?php

 

$name = $_POST['name'];

$lastName = $_POST['lastName'];

$email = $_POST['email'];

$state = $_POST[['state'];

$lastFirstName = "$name" . ' ' . "$lastName";

 

$error = true;

 

if(!isset($_POST['name']) || empty($_POST['name']) || ereg("[0-9!@#$%^&*()]", $name)){

              $error = false;

                      print("Please fill out your name. You either left it blank or used characters that are not allowed!");

              }

           

              else if(!isset($_POST['lastName']) || empty($_POST['lastName']) || ereg("[0-9!@#$%^&*()]", $lastName)){

                      $error = false;

                      print("Please fill out your name. You either left it blank or used characters that are not allowed!");

              }

           

              else if(!isset($_POST['email']) || empty($_POST['email'])

                      || ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", email)){

                      $error = false;

                      print("Please Enter an E-mail");

              }

           

              else if(!isset($_POST['state']) || empty($_POST['state']) || ereg("[0-9!@#$%^&*()]", $state)){

                      $error = false;

                      print("Please Enter a State");

              }

           

              if (! $error){

               

              }

  else {

  print("Hello" .' '. $lastFirstName);

  }

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/91885-new-here-and-new-to-php/
Share on other sites

Hello, and welcome. The thing with elseif statements is the first one to evaluate to true is the only one that will run. You'll want to you plain old if statements.

 

<?php
$name = $_POST['name'];
$lastName = $_POST['lastName'];
$lastFirstName = "$name" . ' ' . "$lastName";
$error = false;
if (!isset($_POST['name']) || empty($_POST['name']) || ereg("[0-9!@#$%^&*()]", $name)) {
  $error = true;
  print("Please fill Out Your Name");
}
             
if (!isset($_POST['lastName']) || empty($_POST['lastName']) || ereg("[0-9!@#$%^&*()]", $lastName)) {
  $error = true;
  print("Please fill Out Your Last Name");
}
             
if (!isset($_POST['email']) || empty($_POST['email'])){
  $error = true;
  print("Please Enter an E-mail");
}
             
if (!isset($_POST['state']) || empty($_POST['state'])){
  $error = true;
  print("Please Enter a State");
}
             
if (!$error) {
  print("Hello" .' '. $lastFirstName);
}
           
?>

 

Also note that you'll want to set $error to true is there is an error. It just makes more sense.

 

Oh, and ps. We have [ code ][ /code ] tags (without the spaces) which enable syntax highlighting on these forums, please use them when posting code.

A few more things to note since you are just starting. Variables do not need to be surrounded in quotes, so this.....

 

$lastFirstName = "$name" . ' ' . "$lastName";

 

should be....

 

$lastFirstName = $name . ' ' . $lastName;

 

Also, there is no need for parents around the arguments to print.

 

print "hello world";

 

Is cleaner and valid.

Please surround all code you post on this forum with


tags. You can go back and modify your post to do that.

 

You will only get individual error reports since you are using if-elseif, not checking each condition separately.

 

Here's how I would do the checks:

<?php
$flds = array('name' => 'Name','lastname' => 'Last Name','email' => 'an E-mail','state' => 'a State');
$errors = array();
foreach ($flds as $fld => $msg_val) {
    switch ($fld) {
            case 'name':
            case 'lastname':
                if (strlen(trim(stripslashes($_POST[$fld]))) == 0 || ereg("[0-9!@#$%^&*()]", $_POST[$fld]))
                       $errors[] = 'Please fill out Your ' . $msg_val;
                break;
            case 'email':
            case 'state':
                strlen(trim(stripslashes($_POST[$fld]))) == 0)
                       $errors[] = 'Please Enter ' . $msg_val;
                break;
    }
}
if (!empty($errors))
   echo implode('<br>', $errors);
else
   echo 'Hello ' . $_POST['name'] . ' ' . $_POST['lastname'];
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.