简体   繁体   English

表单POST之后的PHP重定向

[英]PHP redirect after form POST

i'm making a download button on my website that opens a modal with a request for password and then if the password is right should take the user to the real download. 我在我的网站上制作了一个下载按钮,该按钮会打开一个模态,要求输入密码,然后,如果密码正确,则应将用户带到真正的下载位置。 This is my javascript: 这是我的javascript:

function submit() {

    var postData = $("#passwd").serialize();
    //alert(postData);
    $.ajax({
        type: "POST",
        url: "http://www.redcraft.it/submit.php",
        data: postData,
        success: function(redirect) {
            alert('Submitted')
        }/*,
        error: function() {
            alert('Failure');
        }*/
    });
}

and this is my php: 这是我的PHP:

<?php
function redirect() {
    $data = $_POST["postData"];
    if($data == is_null()) {
        echo "Error";
    } else {
        echo "<script>window.open('http://www.google.com/" + $data + "', '_self')</script>";
    }
}
?>

I'm using echo with a js script in php to try different methods to redirect since header() doesn't work. 由于header()无法正常工作,因此我在PHP中将echo与js脚本一起使用以尝试其他重定向方法。 I'm sure the php is correctly called because i've added a row to create a file when the function is called, and the file is correctly generated. 我确定php是正确调用的,因为在函数调用时我添加了一行来创建文件,并且文件正确生成。

Please, i need your help and don't kill me if i've made some "noobie" errors. 请,我需要您的帮助,如果我犯了一些“ noobie”错误,请不要杀死我。

Edit: 编辑:

This is the html of the modal: 这是模式的html:

<div class="modal fade bs-modal-sm" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-mediom">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Download mondo RedCraft</h4>
                    </div>
                    <div class="modal-body">
                        <p>Per scaricare il mondo della redcraft inserisci la password che trovi nel video di presentazione del download!</p>
                        <div class="control-group">
                            <label class="control-label" for="userid">Password:</label>
                            <div class="controls">
                                <input id="passwd" required="" name="passwordinput" type="input" class="form-control input-medium">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="confirmsignup"></label>
                            <div class="controls">
                                <button id="confirm" onClick="submit()" name="confirmsignup" class="btn btn-primary" data-dismiss="modal">Conferma</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

just add one div in your html 只需在您的html中添加一个div

<div id="outputredirect"></div>

and add below line in your ajax success or replace with your alert 并在ajax成功中添加以下行或替换为警报

if(redirect == "Error"){
   alert(redirect);
} else {
   jQuery('#outputredirect').html(redirect);
}

Your javascript code should redirect to the url returned by the php script : 您的JavaScript代码应重定向到php脚本返回的网址:

function submit() {
    var postData = $("#passwd").serialize();
    //alert(postData);
    $.ajax({
        type: "POST",
        url: "http://www.redcraft.it/submit.php",
        data: postData,
        success: function(redirect) {
            location.href(redirect);
        }/*,
        error: function() {
            alert('Failure');
        }*/
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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