简体   繁体   中英

Display die(); message on the same page as form

I am a beginner with PHP.

Anyway, I have this form:

<form method="post" action="mail.php" id="contactform">
    <div class="stage cf">
        <input type="text" name="contactname" id="contactname" placeholder="Name" class="required" role="input" aria-required="true" />
    </div>
    <div class="stage cf">
        <input type="text" name="email" id="useremail" placeholder="Email" class="required email" role="input" aria-required="true" />
    </div>
    <div class="stage cf">
        <input type="text" name="subject" id="subject" placeholder="Subject" class="required" role="input" aria-required="true" />
    </div>
    <div class="stage cf">
        <textarea rows="5" name="message" id="message" placeholder="Message" class="required" role="textbox" aria-required="true"></textarea>
    </div>                                      
        <input type="submit" value="Send Message" name="submit" id="submitButton" title="SendMessage" />
    <div id="response"></div>
</form>

When the form is sent, I want to display a die message on the same page. The message at this point shows on another page.

This chunk of my mail.php file sends the mail:

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'test@randommail.com'; //Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
    $headers = "From: $email";

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}
die("<span class='success'>Success! Your message has been sent.</span>");

and this Ajax should do the trick:

submitHandler: function(form) {
    $("#send").attr("value", "Sending...");
    $(form).ajaxSubmit({
        success: function(responseText, statusText, xhr, $form) {
            $(form).slideUp("fast");
            $("#response").html(responseText).hide().slideDown("fast");
        }
    });
    return false;
}

die('string')的意思是,停止运行并转储'string',它应该起作用!

it is the same page (mail.php) but you are calling die before the html is displayed so move your die line after the html code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM