idjuradj
idjuradj

Reputation: 1465

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 = '[email protected]'; //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;
}

Upvotes: 0

Views: 664

Answers (2)

islemdev
islemdev

Reputation: 206

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

Upvotes: 0

David
David

Reputation: 914

die('string') means, stop running and dump the 'string' It should work!

Upvotes: 1

Related Questions