表格提交后重定向

时间:2015-02-08 10:36:15

标签: javascript php forms redirect registration

在用户使用“注册”按钮提交表单并在表单将电子邮件发送到antoniya.p之后,如何使此表单将用户重定向到外部站点,例如http://amaze.bg @ amaze.bg用户输入的详细信息?

这是PHP代码(contact_process.php):

<?php

usleep(500000);
$to = "antoniya.p@amaze.bg";
$author = "";
$email = "";
$comment = "";
$class = "FORM CONTACT ";

if (isset($_POST['input_name']))
    $author = $_POST['input_name'];
if (isset($_POST['input_email']))
    $email = $_POST['input_email'];
if (isset($_POST['input_class']))
    $class = $_POST['input_class'];
if (isset($_POST['input_message']))
    $comment = $_POST['input_message'];

$sub="Alumni registration recieved - ".$date1;
       $name=$authorlast."< ".$email." >";

    $msg = '';
    if (@mail($to,$sub,$body,"Content-Type: text/html; charset=iso-8859-1"))
        {
            $msg = 'Your registration was sent successfully';
            //echo '<div class="alert success pngfix">'. $msg .'</div>';
        }
        else{
            $msg = 'Email failed to be sent (mail function not work)';
            //echo '<div class="alert error pngfix">'. $msg .'</div>';
        }

    echo $msg;


?>

这是.js部分:

jQuery.validator.addMethod(
        "math", 
        function(value, element, params) { 
            if (value==='')
                return false;
            return this.optional(element) || value == params[0] + params[1]; 
        },
        jQuery.format("Please enter the correct value for {0} + {1}")
    );
    $('.form-register').validate({
        rules: {
            input_name: {
                minlength: 3,
                required: true
            },
            input_email: {
                required: true,
                email: true
            },
            input_message: {
                minlength: 0,
                required: false
            }
            ,
            submitHandler: function(form) {
            var a=$('.form-register').serialize();
            $.ajax({
                type: "POST",
                url: "contact_process.php",
                data:a,
                complete:function(){
                },
                beforeSend: function() {

                },
                success: function(data){
                    if (data=='success') {
                        $('.form-register').find("input[type=text], textarea").val("");
                        alert('You have successfully registered. Thank you for being active alumni!');
                    } else {
                        alert(data);
                    }
                },
                error : function() {

                }
            });
            return false;
        }
    });


});

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

在您的成功回拨中,添加位置=&#34; http://amaze.bg&#34;

success: function(data) {
    // if you echo "success" in your php script when the mail is sent 
    if (data === 'success') {
        $('.form-register').find("input[type=text], textarea").val("");
        alert('You have successfully registered. Thank you for being active alumni!');
        // Now redirect
        location = "http://amaze.bg"
    } else {
        // if you echo "Some Error Message" from your php script
        alert(data);
    }
},

最终,您应该从php发送正确的标头,以便$ .ajax中的成功挂钩触发200状态代码,并且当遇到400 - 500状态代码时,错误挂钩会在$ .ajax中触发。请查看HTTP Status CodesSending Proper Headers in PHP

答案 1 :(得分:0)

经过Charlie的一些实验和帮助后,我设法找到了代码的确切位置和内容,以便表单将信息发送到服务器,然后将用户重定向到另一个网站。应在.js文件中进行更改,并添加以下行:

location = "http://amaze.bg/"

但它应该添加在.js文件中的“else”下,“alert”处于“成功”状态,以便一切正常工作:

success: function(data){
            if (data=='success') {

            alert(data);

            } else {

            location = "http://amaze.bg/"

            }
        },
            error : function() {

            }