I am using a flight search api. My question is in my form, if I send the form fields to an ajax file as
var data = $('form').serialize();
Is there a way in my ajax file to append serialize() string sent to my api url. Right now my ajax file looks like this and although it works, it is very messy and code needs refactored:
require('lib/Unirest.php');
if(isset($_POST['departureAirport'])){
$customerSessionId = $_POST['customerSessionId'];
$departureAirport = $_POST['departureAirport'];
$destinationAirport = $_POST['destinationAirport'];
$departureDate = $_POST['departureDate'];
$returnDate = $_POST['returnDate'];
$adults = $_POST['adults'];
$getdata = Unirest::get("http://test.api.theapidomain.com/airticket/v1/list.aspx?cid=N7Y5C1&customerSessionId=".$customerSessionId."&departureAirport=".$departureAirport."&destinationAirport=".$destinationAirport."&departureDate=".$departureDate."&returnDate=".$returnDate."&adults=".$adults."", array( "Accept" => "application/json" )
);
I was really hoping for something like this:
$dataSent = //the serialized data sent
$getdata = Unirest::get("http://test.api.theapidomain.com/airticket/v1/list.aspx? + $dataSent, array( "Accept" => "application/json" )
);
here is my jquery
<script type="text/javascript">
$('#myform').submit(function(e){
e.preventDefault();
var data = $('form').serialize();
$.ajax({
type: "POST",
url: "ajax.php",
data: data,
success: function(response){
$("#result").html(response);
}
});
});
</script>