在表单页面上显示表单错误消息

时间:2018-05-20 18:09:29

标签: php

我正在尝试在与表单相同的页面上显示php错误消息,但无法打印此内容。

表单正在运行,错误消息也出现了,但问题只是在另一页上重定向。

这是form.php

<form action="contact_mail.php" method="post" name="contact_Form" id="contactform">

          <div class="row">
            <div class="col-md-12 page_subtitle">Get in Touch</div>
          </div>
          <div class="row mt-30">
            <div class="col-md-12 form-group"><input type="text" value="<?php if (isset($errors['name_co'])): echo $errors; endif; ?>" placeholder="Name" class="form-control" name="name_co" ></div>              
          </div>
          <div class="row">
            <div class="col-md-6 form-group"><input type="text" id="txtEmail" value="" placeholder="Email ID" class="form-control" name="email_co" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,63}$" required /></div>
            <div class="col-md-6 form-group"><input type="text" value="" placeholder="Phone Number" class="form-control" name="mobile" pattern="[789][0-9]{9}" required /></div>
          </div>
          <div class="row">
            <div class="col-md-12 form-group"><textarea cols="45" rows="6" placeholder="Message" class="form-control" name="comments_co" required/></textarea></div>
          </div>
          <div class="row">
            <div class="col-md-12 form-group"><?php 
                            $a_con=rand(0,9);
                            $b_con=rand(0,9);
                        ?>
         Human Test <?php echo $a_con." + ".$b_con?> = 
                        </div>
          </div>
          <div class="row">

         <div class="col-md-12 form-group"><input type="text" value="" placeholder="Human Test" class="form-control" name="value_num" required/>
    <input class="input" name="captcha_num_hidden_contact" id="captcha_num_hidden_contact" type="hidden" value="<?php echo $a_con+$b_con;?>"/>
            </div>
          </div>
          <div class="row">
              <div class="col-md-12 form-group"><input type="submit"   name="submit" class="btn btn-submit" /></div>
          </div>
          </form>

这是 contact_mail.php

<?php

$errors = array();    // array to hold validation errors
$data = array();        // array to pass back data
//error_reporting(0);

// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
 if (isset($_POST['submit'])){   
if (empty($_POST['name_co']))
  $errors['name_co'] = 'Name is required.';
if (empty($_POST['email_co']))
    $errors['email_co'] = 'Email is required.';

if (empty($_POST['mobile'])){
    $errors['mobile'] = 'Mobile no is required.';
} else if(strlen((string)$_POST['mobile']) < 10){

    $errors['mobile'] = 'Please enter a valid Mobil No.';
}

if (empty($_POST['comments_co']))
    $errors['comments_co'] = 'Comments is required.';


if (empty($_POST['value_num'])) {
    $errors['value_num'] = 'Human Test is required.';
}
else if ($_POST['captcha_num_hidden_contact'] != $_POST['value_num']) {
    $errors['captcha_num_hidden_contact'] = 'Please enter the correct result.';

}
 }
// return a response ===========================================================

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {

    // if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;

} else {

    // if there are no errors process our form, then return a message

    // DO ALL YOUR FORM PROCESSING HERE
    // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)



    $email_subject = 'Query From Contact Us';




//    $message = '<div><strong>Query From Contact Us : </strong></div>';
//    $message .= '<div>---------------------------------------- </div>';
//    $message .= '<div><strong>Name: </strong>' . $_POST['name_co'] . '</div>';
//    $message .= '<div><strong>Email: </strong>' . $_POST['email_co'] . '</div>';
//    $message .= '<div><strong>Service: </strong>' . $_POST['option_type'] . '</div>';
//    $message .= '<div><strong>Subject: </strong>' . $_POST['subject_co'] . '</div>';
//    $message .= '<div><strong>Mobile: </strong>' . $_POST['mobile'] . '</div>';
//    $message .= '<div><strong>Comments: </strong>' . $_POST['comments_co'] . '</div>';


    $email_subject1 = 'Acknowledgement';


    $headers1 = "MIME-Version: 1.0\r\n";
    $headers1 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers1 .= "From: "."\r\n";
    $mailstatus1 = mail($_POST['email_co'], $email_subject1, $message1, $headers1, "");


    // show a message of success and provide a true success variable
    $data['success'] = true;
    $data['message'] = 'Your Query has been successfully submit. We will contact you very soon!!';
    header("location:thank-you.php");
      exit;
}
//echo json_encode($data);
// return all our data to an AJAX call

我的主要问题是我想在表单页面上打印错误消息,但我无法打印它。

2 个答案:

答案 0 :(得分:0)

解决方案很简单。您只需输出具有以下错误的数组

if (!empty($errors)) {

你可以这样做:

echo implode('<br>', $data['errors']);

implode()会将数组放在一个字符串中。
当它被内爆时放在每个数组元素之间。

答案 1 :(得分:0)

如评论中所述,我在一个页面上实现了您的代码。 改变了你的

<form action="contact_mail.php" method="post" name="contact_Form" id="contactform">

<form target="_self" method="post" name="contact_Form" id="contactform" enctype="multipart/form-data">

并添加了打印错误,如@Bernhard答案

  echo implode('<br>', $data['errors']);  

此行中也改变了模式

<div class="col-md-6 form-group"><input type="text" value="" placeholder="Phone Number" class="form-control" name="mobile" pattern="[123456789]{9}" required /></div>

我想说的是,当你准备好考虑用户输入验证时,不仅仅是html和使用库发送电子邮件,如PHPMailer。

这是完整的代码

<?php 


$errors = array();    // array to hold validation errors
$data = array();        // array to pass back data
//error_reporting(0);

// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
 if (isset($_POST['submit'])){   
if (empty($_POST['name_co']))
  $errors['name_co'] = 'Name is required.';
if (empty($_POST['email_co']))
    $errors['email_co'] = 'Email is required.';

if (empty($_POST['mobile'])){
    $errors['mobile'] = 'Mobile no is required.';
} else if(strlen((string)$_POST['mobile']) < 10){

    $errors['mobile'] = 'Please enter a valid Mobil No.';
}

if (empty($_POST['comments_co']))
    $errors['comments_co'] = 'Comments is required.';


if (empty($_POST['value_num'])) {
    $errors['value_num'] = 'Human Test is required.';
}
else if ($_POST['captcha_num_hidden_contact'] != $_POST['value_num']) {
    $errors['captcha_num_hidden_contact'] = 'Please enter the correct result.';

}
 }
// return a response ===========================================================

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {

    // if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;

    echo implode('<br>', $data['errors']);

} else {

    // if there are no errors process our form, then return a message

    // DO ALL YOUR FORM PROCESSING HERE
    // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)



    $email_subject = 'Query From Contact Us';




//    $message = '<div><strong>Query From Contact Us : </strong></div>';
//    $message .= '<div>---------------------------------------- </div>';
//    $message .= '<div><strong>Name: </strong>' . $_POST['name_co'] . '</div>';
//    $message .= '<div><strong>Email: </strong>' . $_POST['email_co'] . '</div>';
//    $message .= '<div><strong>Service: </strong>' . $_POST['option_type'] . '</div>';
//    $message .= '<div><strong>Subject: </strong>' . $_POST['subject_co'] . '</div>';
//    $message .= '<div><strong>Mobile: </strong>' . $_POST['mobile'] . '</div>';
//    $message .= '<div><strong>Comments: </strong>' . $_POST['comments_co'] . '</div>';


    $email_subject1 = 'Acknowledgement';


    $headers1 = "MIME-Version: 1.0\r\n";
    $headers1 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers1 .= "From: "."\r\n";
    $mailstatus1 = mail($_POST['email_co'], $email_subject1, $message1, $headers1, "");


    // show a message of success and provide a true success variable
    $data['success'] = true;
    $data['message'] = 'Your Query has been successfully submit. We will contact you very soon!!';
    //header("location:thank-you.php");

}


?>


<form target="_self" method="post" name="contact_Form" id="contactform" enctype="multipart/form-data">

          <div class="row">
            <div class="col-md-12 page_subtitle">Get in Touch</div>
          </div>
          <div class="row mt-30">
            <div class="col-md-12 form-group"><input type="text" value="<?php if (isset($errors['name_co'])): echo $errors; endif; ?>" placeholder="Name" class="form-control" name="name_co" ></div>              
          </div>
          <div class="row">
            <div class="col-md-6 form-group"><input type="text" id="txtEmail" value="" placeholder="Email ID" class="form-control" name="email_co" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,63}$" required /></div>
            <div class="col-md-6 form-group"><input type="text" value="" placeholder="Phone Number" class="form-control" name="mobile" pattern="[123456789]{9}" required /></div>
          </div>
          <div class="row">
            <div class="col-md-12 form-group"><textarea cols="45" rows="6" placeholder="Message" class="form-control" name="comments_co" required/></textarea></div>
          </div>
          <div class="row">
            <div class="col-md-12 form-group"><?php 
                            $a_con=rand(0,9);
                            $b_con=rand(0,9);
                        ?>
         Human Test <?php echo $a_con." + ".$b_con?> = 
                        </div>
          </div>
          <div class="row">

         <div class="col-md-12 form-group"><input type="text" value="" placeholder="Human Test" class="form-control" name="value_num" required/>
    <input class="input" name="captcha_num_hidden_contact" id="captcha_num_hidden_contact" type="hidden" value="<?php echo $a_con+$b_con;?>"/>
            </div>
          </div>
          <div class="row">
              <div class="col-md-12 form-group"><input type="submit"   name="submit" class="btn btn-submit" /></div>
          </div>
          </form>