0

I have a php and html code. I have 4 input for email send (name, to, subject, message) and i have exception validate. Now my coding working: if first input is empty then show error message, if second input is empty (and first is not empty) show error message, etc... I want to show every error message if i click on SEND not one by one with php exception. Any ideas how can I fix it ?

The PHP code with exceptions handling:

if (isset($_POST['Send'])) {
    class emailException extends Exception {}
    class email_validException extends Exception {}
    class subjectException extends Exception {}
    class messageException extends Exception {}

    $name = $_POST['name'];
    $to = $_POST['to'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    try {
        if($name == "") {
            throw new Exception("Töltsd ki a mezőt(1)!");
        }
        if($to == "") {
            throw new emailException("Töltsd ki a mezőt(2)!");
        }
        // Email cim ellenőrzése
        if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
            throw new email_validException("Hibás email cím!");
        }
        if($subject == "") {
            throw new subjectException("Töltsd ki a mezőt(3)!");
        }
        if($message == "") {
            throw new messageException("Töltsd ki a mezőt(4)!");
        }
        if ($name != "" && $to != "" && $subject != "") {
            echo "Email elküldve";
            $name = "";
            $to = "";
            $subject = "";
            $message = "";
        }
    }
    catch(emailException $e1) {
        $error_mail = $e1->getMessage();
    }
    catch(email_validException $e1) {
        $error_mail_valid = $e1->getMessage();
    }
    catch(subjectException $e2) {
        $error_subj = $e2->getMessage();
    }
    catch(messageException $e3) {
        $error_message = $e3->getMessage();
    }
    catch(Exception $e) {
        $error = $e->getMessage();
    }
}

HTML input form:

<form name="myForm" onsubmit="//return validateForm();" method="post" action="feladat9.php">
    <p><input type="text" name="name" placeholder="Név" value="<?php echo $name; ?>"><label id="error_name"><?php echo $error ?></label></p>
    <p><input type="text" name="to" placeholder="Címzett" value="<?php echo $to; ?>"><label id="error_email"><?php
            echo "$error_mail $error_mail_valid";?></label></p>
    <p><input type="text" name="subject" placeholder="Tárgy" value="<?php echo $subject; ?>"><label id="error_subject"><?php echo $error_subj ?></label></p>
    <p><input type="text" name="message" placeholder="Üzenet" value="<?php echo $message; ?>"><label id="error_message"></label><?php echo $error_message ?></p>
    <input type="submit" value="Send" id="send" name="Send">
</form>
jean-max
  • 1,640
  • 1
  • 18
  • 33
  • 2
    save the exception message in array instead of throwing it immediately, throw all the exceptions when done with the conditionals checking – funsholaniyi Jan 02 '17 at 09:55
  • `$name = isset($_POST['name']) ? $_POST['name'] : "" ; $to = isset($_POST['to']) ? $_POST['to'] : "" ; $subject = isset($_POST['subject']) ? $_POST['subject'] : "" ; $message = isset($_POST['message']) ? $_POST['message'] : "" ;` – Eugen Jan 02 '17 at 09:59
  • @Eugen Where shoud i insert this code ? –  Jan 02 '17 at 10:01

1 Answers1

1

If you want to show all the errors, you should not use 'throw new exception' concept, because exception stop next the execution to jumps to catch section.

You can try like this:

if(isset($_POST['Send']))
{

    $name = $_POST['name'];
    $to = $_POST['to'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $errors = array();

    if($name == "")
    {
         $errors['error'] = "Töltsd ki a mezőt(1)!";
    }
    if($to == "")
    {
        $errors['error_mail'] = "Töltsd ki a mezőt(2)!";
    }
    // Email cim ellenőrzése
    if (!filter_var($to, FILTER_VALIDATE_EMAIL))
    {
           $errors['error_mail_valid'] = "Hibás email cím!";
    }
    if($subject == "")
    {

        $errors['error_subj'] = "Töltsd ki a mezőt(3)!";
    }
    if($message == "")
    {
        $errors['error_message'] = "Töltsd ki a mezőt(4)!";
    }
    if ($name != "" && $to != "" && $subject != "")
    {
        echo "Email elküldve";

        $name = "";
        $to = "";
        $subject = "";
        $message = "";
    }

}

and $errors variable can be used to show the erros.

Pawan Developers
  • 377
  • 4
  • 16
  • I need to use exceptions –  Jan 02 '17 at 10:05
  • exception stop next the execution to jumps to catch section. so if there is exception for empty $name, then other code execution will stops and it jumps to catch section. So using exception you can't show all errors at once. – Pawan Developers Jan 02 '17 at 10:12
  • so i cant drop all error message at the same time? some solution shoud be work –  Jan 02 '17 at 10:19
  • Can you explain why you need to use exceptions? – CUGreen Jan 02 '17 at 10:30