1

This is a php file called sendmail.php that validates and sends an email from a contact form (which is included below)

The trouble I'm having is when the alert is clicked it redirects the page to sendemail.php because, I'm assuming, of form action=sendmail.php

How do I resolve this so it stays on the contact page and the user can pick up where they left off? I have tried using header like for the thank you page but to no avail!

if (isset($_POST["submit"])) {
    $validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]);
    if ($validate==FALSE) {
        $error_msg = "Please fill out all information";
        echo '<script type="text/javascript">
            alert("'.$error_msg.'");
        </script>';
    } else {
        $mailcheck = spamcheck($_POST["email"]);
        if ($mailcheck==FALSE) {
            $error_msg = "Invalid email address";
            echo '<script type="text/javascript">
            alert("'.$error_msg.'");
        </script>';
        } else {
        $email = $_REQUEST['email'] ;
        $message = $_REQUEST['message'] ;
        $subject = $_REQUEST['subject'] ;

        mail( "[email protected]", $subject, $message, "From: $email" );
        header( "Location: http://www.thankyou.html" );
        }
    }
}

<form method="post" action="sendmail.php">
    <input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br>
    <input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br>
    <input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br>
    <textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br>
    <input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br>
</form>
6
  • Just have everything happen on this page. Move the code from the sendmail.php over to this page (or include on submit). Commented Sep 27, 2014 at 4:10
  • use header in the sendmail.php after success Commented Sep 27, 2014 at 4:12
  • Arif - I tried this but I just kept getting redirected to sendmail.php. The header works for success but for some reason not after a validation error Commented Sep 27, 2014 at 4:14
  • Rasclatt - What will I need to do with the action class in that instance? Will # keep me on the page or is there a better way of doing it? Commented Sep 27, 2014 at 4:15
  • i think you got your answer Commented Sep 27, 2014 at 4:15

4 Answers 4

1

Try this and Read the comment. Comment: Add this line each time the validation failed window.location= "Your-Form-File-Name.php";

if (isset($_POST["submit"])) {
    $validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]);
    if ($validate==FALSE) {
        $error_msg = "Please fill out all information";
        echo '<script type="text/javascript">
            alert("'.$error_msg.'");
                window.location= "Your-Form-File-Name.php"; **//This line added by me**
****
        </script>';
    } else {
        $mailcheck = spamcheck($_POST["email"]);
        if ($mailcheck==FALSE) {
            $error_msg = "Invalid email address";
            echo '<script type="text/javascript">
            alert("'.$error_msg.'");
                window.location= "Your-Form-File-Name.php"; ////This line added by me
**//Added above line**
        </script>';
        } else {
        $email = $_REQUEST['email'] ;
        $message = $_REQUEST['message'] ;
        $subject = $_REQUEST['subject'] ;

        mail( "[email protected]", $subject, $message, "From: $email" );
        header( "Location: http://www.thankyou.html" );
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The redirect to thank you works ok. That's what was confusing me, I didn't know why header worked for that and not for the other part. Cheers
Is there a way of doing this so it doesn't clear the data from the form? I'm guessing AJAX? Not sure how to use it
Can I just repopulate the form with the variables I have already in the php file?
1

I don't have experience with php but i think you need to returen false when form is not validated.

 if ($validate==FALSE) {
    $error_msg = "Please fill out all information";
    echo '<script type="text/javascript">
        alert("'.$error_msg.'");
    </script>';
   return false;//Form is not validated stop from being submitted.
}

1 Comment

I thought this was the case earlier but it didn't solve the redirect problem
1

Validate form with jQuery BEFORE user submits form, make the "Thank you" happen on same page on successful submit. This way you just use PHP to process email send.

<?php
    if(isset($_POST["submit"])) {
            $email      =   $_POST['email'] ;
            $message    =   strip_tags($_POST['message']);
            $subject    =   strip_tags($_POST['subject']);

            if(mail("[email protected]", $subject, $message, "From: $email")) { ?>
            <h3>Thank you, come again!</h3>
        <?php   }
            else { ?>
            <h3>An error occurred. My bad!</h3>
            <?php }
        } ?>

<form method="post" action="contact.php" id="emailer">
    <input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br>
    <input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br>
    <input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br>
    <textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br>
    <input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br>
</form>

<style>
.error { color: red; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>

<script>
$(document).ready(function() {
    // On submit, validate below fields.
    $("#emailer").validate({
        rules: {
            name: "required",
            email: {
                    required: true,
                    email: true
                },
            subject: "required",
            message: "required"
        },
        messages: {

            name: "required",
            email: {
                    required: "Required",
                    email: "must be valid email"
                },
            subject: "required",
            message: "required"
        }
    });
});
</script>

1 Comment

I really like this way of doing it, it handles the validation nicely. I'll keep this for reference. Thanks for the help!
0

This is sendmail.php, right? IF you want to redirect to contact.php just header() to there. You set form action to sendmail.php that means you set to this file too, it will do the same if you delete action prop from form tag. Just change your header value to contact.php or

print 'location.href="'.$url_to_contact_php.'"';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.