Paul Noris
Paul Noris

Reputation: 103

Ajax redirect to another page after success with POST

I need to validate a form, I pass all the values with ajax to a php file where I valid some dates, after it is successful I need to redirect to another php file but with a POST, it is possible ?

I dont know if its the best way to do that so I'm open to any suggestions

This is my Ajax code:

$("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;
});

And this is my php file "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);
?>

Upvotes: 0

Views: 964

Answers (1)

Roamer-1888
Roamer-1888

Reputation: 19288

A typical pattern would be :

$("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);
    });
});

Building your code on this pattern should be simple. You just need to make a few changes, eg ...

  • flesh out the ajax params.
  • replace my if(...) {...} else {...} with your switch/case structure.
  • make sure your form has an <input id="payment" /> field (or whatever).
  • make sure your page has an id="message" element (or whatever).

If you don't want to submit the whole of the original form to "payment.php" then you can build/submit a separate (off-screen) form.

Upvotes: 2

Related Questions