Reputation:
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>
Upvotes: 0
Views: 1094
Reputation: 377
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.
Upvotes: 1