0

How can I write a select case with an array to check form validation?

this is my code:

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$array = array($name,$email,$message);

switch($array[]) {
    case empty($array[0]):
        error = "name";
        break;
    case empty($array[1]):
        error =  "email";
        break;
    case empty($array[2]):
        error = "message";
}

Then, I would like to write code to have this result:
if name is empty: "Please fill in your name"
if email is empty: "Please fill in your email"
if name and email is empty: "Please fill your name and email"
if name and email and message is empty: "Please fill in your name, email and message"

1
  • If you want to concat your error messages, you should better use if - if else statements Commented Jun 6, 2013 at 14:56

5 Answers 5

3

You want to concat your messages, so better use if statements:

$error = "Please fill in: ";

if (empty($array[0]))
    $error .= "name ";
if (empty($array[1]))
    $error .= "email ";
if (empty($array[2]))
    $error .= "message ";

The .= will concat the string to the existing one.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this for a grammatically correct solution:

$empty = array();
$fields = array('name', 'email', 'message');
foreach ($fields as $key => $value){
    if(empty($_POST[$value])) $empty[] = $value;
}
$error_msg = '';
$count = count($empty);
$cnct = ', ';
if ($count > 0){
    $error_msg = 'Please fill in your ';
}
foreach ($empty as $key => $value){
    if ($key == $count - 2){
        $cnct = ' and ';
    }elseif($key == $count - 1){
        $cnct = '.';
    }
    $error_msg .= $value.$cnct;
}

1 Comment

Thank you very much! This is what I was searching for :)
1

You can simply try:

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$error="Please fill in your ";

$array = array('name'=>$name,'email'=>$email,'message'=>$message);
foreach($array as $key=>$value){
    if(empty($value)){
        $error.=','.$key;
    }
}

Comments

0

You can't use a variable expression in case statement of switch block.

A switch case must have a constant expression in many languages including php. So, something like a variable or function call doesn't work.

You better use conditionals for this.

Your code is also missing $ symbol for variable error.

Do this instead:

  $name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$array = array($name,$email,$message);

    $error="Please fill in your ";
    if(empty($array[0])){
        $error.= "\nname";
        }
    if(empty($array[1])){
        $error.="\nemail";
        };
    if(empty($array[2])){
        $error.= "\nmessage";
    }
   echo $error;

Comments

0

You should simply write:

$error = "Please fill in: ";

if (empty($array[0]))
    $error.= "name ";
if (empty($array[1]))
    $error.=  "email ";
if (empty($array[2]))
    $error.= "message";

A switch isn't made for what you want to do.

4 Comments

This will only display the message for one field. Need to be able to error on more than one.
case would do the same. If you want to error on more then one then drop the elses and append the error to the string
@BernhardPoiss: Actually no. It's a coincidence, I started editing it right after the first comment and was just ... distracted with something else before posting the edit.
@BernhardPoiss Doesn't look like it was a copy-and-paste, as it has a missing .= operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.