使用POST成功后,Ajax重定向到另一个页面

时间:2017-06-25 20:16:49

标签: javascript php jquery ajax

我需要验证一个表单,我将带有ajax的所有值传递给一个php文件,其中我有效日期,成功之后我需要重定向到另一个php文件但是有一个POST,它可能吗?

我不知道这是否是最佳方式,所以我可以接受任何建议

这是我的Ajax代码:

$("form#Reservation").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
    url: 'confirmreservation.php',
    type: 'POST',
    data: formData,
    async: false,
    success: function (data) {

        values=data.split('|');
        one=values[0];
        two=values[1];
        switch (one) 
        { 
            case '1': 
                // here I need something like this
                // POST(two) to payment.php
                break;
            case '2': 
                $("#reservation-fields" ).css("visibility","visible");
                break;
            case '3':
                $("#reservation-fields" ).css("visibility","hidden");
                $("#personal-fields" ).css("visibility","visible");
                break;                  
            case '4': 
                $("#personal-fields" ).css("visibility","hidden");
                $("#arrival-fields" ).css("visibility","visible");
                break;
            case '5': 
                $("#arrival-fields" ).css("visibility","hidden");
                $("#departure-fields" ).css("visibility","visible");
                break;
            case '6': 
                $("#departure-fields" ).css("visibility","hidden");
                $("#details-fields" ).css("visibility","visible");
                break;                                                       
            default:
            alert(data);
        }
    },
    cache: false,
    contentType: false,
    processData: false
});

return false;
});

这是我的php文件" confirmreservation.php"

<?php
require "conexion.php";
require "functions.php";

if ($_POST['service']==null||$_POST['destination']==null||$_POST['people']==null||$_POST['vehicle']==null)
{
    echo "2|0";
}
else
{

    if($_POST['service'] == 1)
    {
        if ($_POST['fname']==null||$_POST['lname']==null||$_POST['email']==null||$_POST['phone']==null)
        {
            echo "3|0";
        }
        else
        {

            if ($_POST['arrival-date']==null||$_POST['arrival-time']==null||$_POST['arrival-airline']==null||$_POST['arrival-flight']==null)
            {
                echo "4|0";
            }
            else
            {
                if ($_POST['departure-date']==null||$_POST['departure-time']==null||$_POST['departure-pick']==null||$_POST['departure-airline']==null||$_POST['departure-flight']==null)
                {
                    echo "5|0";
                }
                else
                {
                    if ($_POST['message']==null||$_POST['payment']==null)
                    {
                        echo "6|0";
                    }
                    else
                    {
                        $ID = 0;
                        $Consult = $Conexion->query('SELECT ID_Client FROM clients ORDER BY ID_Client DESC LIMIT 1');
                        while ($Fila = $Consult->fetch_assoc()) { 
                            $ID =  $Fila['ID_Client'] + 1; 
                        }
                        if($ID == 0){   $ID = 1; }  
                        if($_POST['lang'] == "sp") { 
                            $Arrival = Formato_es_to_date($_POST['arrival-date']); 
                            $Departure = Formato_es_to_date($_POST['departure-date']); 
                        } elseif($_POST['lang'] == "en") 
                        {
                            $Arrival = Formato_en_to_date($_POST['arrival-date']); 
                            $Departure = Formato_en_to_date($_POST['departure-date']); 
                        } 

                        $Conexion->autocommit(false);
                        $Queries=true; 

                        $Conexion ->query("INSERT INTO clients 
                            (ID_client,Client_fname,Client_lname,Client_email,Client_phone) VALUES 
                            ('".$ID."','".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['phone']."')") ? null : $Queries=false;

                        $Conexion ->query("INSERT INTO reservation 
                            (Reserv_service,Reserv_destination,Reserv_people,Reserv_vehicle,Reserv_client,Reserv_arrival_date,Reserv_arrival_time,Reserv_arrival_airline,Reserv_arrival_flight,Reserv_departure_date,Reserv_departure_time,Reserv_departure_airline,Reserv_departure_flight,Reserv_departure_pick,Reserv_message,Reserv_payment) VALUES 

                            ('".$_POST['service']."','".$_POST['destination']."','".$_POST['people']."','".$_POST['vehicle']."','".$ID."','".$Arrival."','".$_POST['arrival-time']."','".$_POST['arrival-airline']."','".$_POST['arrival-flight']."','".$Departure."','".$_POST['departure-time']."','".$_POST['departure-airline']."','".$_POST['departure-flight']."','".$_POST['departure-pick']."','".$_POST['message']."','".$_POST['payment']."')") ? null : $Queries=false;

                        if($Queries == false)
                        {
                            $Conexion->rollback();
                            echo "Error, please contact the provider";
                        }

                        $Conexion->commit();
                        echo "1|".$ID;
                    }
                }
            }
        }
    }
}

mysqli_close($Conexion);
?>

1 个答案:

答案 0 :(得分:2)

典型的模式是:

$("form#Reservation").submit(function(e) {
    e.preventDefault();
    var form = this;
    $.ajax({...}).then(function(data, textStatus, jqXHR) {
        if(data.valid) { // some test or other
            $("input#payment").val(data.someProperty); // put value in some (hidden) form field.
            form.submit(); // the form will submit in accordance with the <form> tag's action and method attributes.
        } else {
            $("#message").text('validation failed');
        }
    }, function(jqXHR, textStatus, errorThrown) {
        $("#message").text(textStatus || errorThrown);
    });
});

在此模式上构建代码应该很简单。您只需进行一些更改,例如......

  • 充实了ajax params。
  • 用您的开关/案例结构替换我的if(...) {...} else {...}
  • 确保您的表单中包含<input id="payment" />字段(或其他)。
  • 确保您的网页包含id="message"元素(或其他)。

如果您不想将整个原始表单提交给&#34; payment.php&#34;然后你可以建立/提交一个单独的(屏幕外)表格。