在线提交表单,在新页面PHP上显示消息

时间:2018-07-08 01:02:17

标签: php

提交后,请您协助我在联系表格的同一页上显示成功/错误消息吗?

表单在提交后发送电子邮件,但是成功消息/错误消息出现在新页面上:

This is the how the message appears

以下是代码:

HTML:

<form action="php/contact.php" method="post" name="sentMessage" id="contactForm" novalidate>
                <div class="col-lg-6 col-md-6 col-sm-6 contact-agile1">    
                    <div class="control-group form-group">
                        <div class="controls">
                            <label class="contact-p1">Full Name:</label>
                            <input type="text" class="form-control" name="name" id="name" required=data-validation-required-message="Please enter your name.">
                            <p class="help-block"></p>
                        </div>
                    </div>  
                    <div class="control-group form-group">
                        <div class="controls">
                            <label class="contact-p1">Phone Number:</label>
                            <input type="tel" class="form-control" name="phone" id="phone" required=data-validation-required-message="Please enter your phone number.">
                            <p class="help-block"></p>
                        </div>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-sm-6 contact-agile2">
                    <div class="control-group form-group">
                        <div class="controls">
                            <label class="contact-p1">Email Address:</label>
                            <input type="email" class="form-control" name="email" id="email" required=data-validation-required-message="Please enter your email address.">
                            <p class="help-block"></p>
                        </div>
                    </div>
                    <div class="control-group form-group">
                        <div class="controls">
                            <label class="contact-p1">Subject:</label>
                            <input type="text" class="form-control" name="subject" id="subject" required=data-validation-required-message="Please enter Subject.">
                            <p class="help-block"></p>
                        </div>
                    </div>
                </div>
                <div class="col-lg-12"> 
                    <div class="control-group form-group">
                        <div class="controls">
                            <label class="contact-p1">Message:</label>
                            <textarea rows="10" cols="100" class="form-control" name="message" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
                            <p class="help-block"></p>
                        </div>
                    </div>
                    <div id="success"></div>
                    <!-- For success/fail messages -->
                    <button type="submit" class="btn btn-primary">Send Message</button>
                </div>
                <div class="clearfix"></div>    
            </form>

PHP:

?php

    $to = 'email@example.co.za';  // our email here

    $errors = array();
    // print_r($_POST);

    // Check if name has been entered
    if (!isset($_POST['name'])) {
        $errors['name'] = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errors['email'] = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!isset($_POST['message'])) {
        $errors['message'] = 'Please enter your message';
    }

        //Check if phone has been entered
    if (!isset($_POST['phone'])) {
        $errors['phone'] = 'Please enter your Telephone';
    }

    //Check if subject has been entered
    if (!isset($_POST['subject'])) {
        $errors['subject'] = 'Please enter your Subject';
    }





    $errorOutput = '';

    if(!empty($errors)){

        $errorOutput .= '<div class="alert alert-danger alert-dismissible" role="alert">';
        $errorOutput .= '<button type="submit" class="submit" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';

        $errorOutput  .= '<ul>';

        foreach ($errors as $key => $value) {
            $errorOutput .= '<li>'.$value.'</li>';
        }

        $errorOutput .= '</ul>';
        $errorOutput .= '</div>';

        echo $errorOutput;
        die();
    }



    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $phone = $_POST['phone'];
    $subject_second = $_POST['subject'];
    $from = $email;
    $subject = 'Enquiry From Website Form:';

    $body = "From: $name\n E-Mail: $email\n Phone: $phone\n Subject: $subject\n Message:\n $message";



    //send the email
    $result = '';
    if (mail ($to, $subject, $body)) {
        $result .= '<div class="alert alert-success alert-dismissible" role="alert">';
        $result .= '<button type="submit" class="submit" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
        $result .= 'Thank You! We will reply back';
        $result .= '</div>';

        echo $result;
        die();
    }

    $result = '';
    $result .= '<div class="alert alert-danger alert-dismissible" role="alert">';
    $result .= '<button type="submit" class="submit" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
    $result .= 'Something went wrong. Please try again. Thank you';
    $result .= '</div>';

    echo $result;
    die();


?>

1 个答案:

答案 0 :(得分:0)

尝试将所有代码(html和php)添加到一个.php文件中。然后像这样编辑“表单标签”

Something