PNR Status Script using PHP, jQuery and Ajax

PNR Status Script using PHP, jQuery and Ajax

PNR Status Script:

Update: Demo may not work as expected, because the testing key provided by the “railwayapi.com” got expired.

 

You can create your own key and substitute that in the script and run to get the expected results / PNR status.

 

Are you planning to develop any PNR alert, status website or any mobile apps ? (or) planning to integrate the PNR status script to your existing website. ?

 

We have provided here how to get the PNR status programmatically using third party API’s and provided the complete source code of the PNR Status Script.

we have developed PNR status script using PHP, jQuery and Ajax and also, JSON and Bootstrap 3.

 

PNR Status script featured image

 

PNR Status Free API’s:

1. eRail PNR API

2. Rail PNR API

3. Railway API

4. Mashape

 

demo
download

Out of the above free API’s we are going to develop a PNR status script with railway API.

 

PNR status using Java Program, now you can create your own API for pnr status,

PNR Live Status using Java

 

 

Requirement:

Railway API Key [You can register here and get the API key to your email.]

 

 

Railway API :

[sociallocker]

Syntax:

http://api.railwayapi.com/pnr_status/pnr/<pnr no>/apikey/<apikey>/

 

With Sample Data:
http://api.railwayapi.com/pnr_status/pnr/4728629631/apikey/7pqu19289191/

here,

7pqu19289191 - Railway API key and 4728629631 - 10 digit PNR number

 

 

Required Imports:

jQuery.js:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

Bootstrap JS & CSS [We have used Bootstrap to make it responsive].


<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.4/flatly/bootstrap.min.css" rel="stylesheet">

 

BlockUI.js [to block the UI when AJAX method called and to display the Please Wait… message]


<script src="http://malsup.github.io/jquery.blockUI.js"></script>

 

 

 

HTML for Text box and search button:


<div class="search">
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1 search-header">
<div class="form-section searchDiv">
<div class="row">

<div class="col-md-10">
<div class="form-group">

<input type="text" class="form-control" id="PNR_NUM" placeholder="Enter the 10digit PNR Number" >
</div>
</div>

<div class="col-md-2">
<button type="button" class="btn btn-default btn-primary" onclick="loadPNRStatus();">Get PNR Status</button>
</div>

</div>
</div>
</div>
</div>
</div>
</div>

 

Scripts:

loadPNRStatus():
We are passing the 10digit PNR Number through AJAX call to the Railway API and get the PNR status without refreshing the page.

<html>

<script>

function loadPNRStatus(){
var searchN = $(‘#PNR_NUM’).val();
if(searchN !=””){
$.ajax( {
type: “POST”,
url: “getPNRStatus.php”,
data:{pnrQ: searchN },
beforeSend: function() {
$.blockUI({ css: {
border: ‘none’,
padding: ’15px’,
backgroundColor: ‘#000’,
‘-webkit-border-radius’: ’10px’,
‘-moz-border-radius’: ’10px’,
opacity: .5,
color: ‘#fff’
} });
},
success:function(htmlResp) {
$(‘div.pnrStatusLoad’).html(htmlResp);
$.unblockUI();
}
});
}
}

</script>

</html>

 

Script to accept only numbers and pressing enter key to get the PNR Status:


// if enter key pressed after pasting/typing PNR status Ajax method will be called

$('#PNR_NUM').on('keypress', function (event) {
if(event.which === 13){
loadPNRStatus();
}

// return false if user enters other than numbers
if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
//display error message
return false;
}
});

 

getPNRStatus.php:

To get the current status of the given PNR.


<?php
$json = '';
if(isset($_POST['pnrQ'])){
$searchParam = $_POST['pnrQ'];
$replacedStr = str_replace(" ","",$searchParam);
$url = 'http://api.railwayapi.com/pnr_status/pnr/'.$replacedStr.'/apikey/YOUR_RAILWAY_API_KEY/';
$content = file_get_contents($url);
$json = json_decode($content, true);
}else{
echo "something went wrong, please notify to admin [Naveen@ngdeveloper.com]";
}
$dateOfJourney = $json['doj'];
$pnr = $json['pnr'];
$fromStation = $json['from_station']['name'];
$fromStationCd = $json['from_station']['code'];
$toStation = $json['to_station']['name'];
$toStationCd = $json['to_station']['code'];
$trainNum = $json['train_num'];
$trainName = $json['train_name'];
$currStatus = $json['passengers'][0]['current_status'];
$bookingStatus = $json['passengers'][0]['booking_status'];
$isChartPrep = $json['chart_prepared'];
$isChartPrepd = ($isChartPrep=='N') ? 'NO' : 'YES';
$cls = $json['class'];
$totPass = $json['total_passengers'];
?>
<div class="col-md-6 col-md-offset-3">
<div class="row">
<li class="col-md-6 list-group-item " style="font-size:20px;text-align:center;"> PNR </li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $pnr; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Date of Journey</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $dateOfJourney; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> From </li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $fromStation; ?> [<?php echo $fromStationCd; ?>]</li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> TO</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $toStation; ?> [<?php echo $toStationCd; ?>]</li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Train Number</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $trainNum; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Train Name</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $trainName; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Booking Status</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $bookingStatus; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Current Status</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $currStatus; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;">Chart Prepared</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $isChartPrepd; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;">Class</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $cls; ?></li>
</div>
<div class="row">
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;">Total Passengers</li>
<li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $totPass; ?></li>
</div>
</div>

 

 

Custom Styles:


<style type="text/css">

@media (max-width: 480px) {
body {
padding-bottom: 40px;
}

.carousel {
height: 200px !important;
margin-bottom: 60px;
}
.carousel .item {
height: 200px !important;
background-color: #777;
}
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 200px !important;
}
.search {
margin-top: -25%;
}
.search .form-section{
background:rgba(0,0,0,0.6);
border: 2px solid #414141;
border-radius: 5px;
padding: 10px;
}

}

body {
padding-bottom: 40px;
}

.carousel {
height: 400px;
margin-bottom: 60px;
}
.carousel .item {
height: 400px;
background-color: #777;
}
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 400px;
}
.search {
margin-top: -25%;
}
.search .form-section{
background:rgba(0,0,0,0.6);
border: 2px solid #414141;
border-radius: 5px;
padding: 10px;
}

</style>

[/sociallocker]

Output:

 

PNR Status Script Demo

 

 

4,374 total views, 3 views today

18 thoughts on “PNR Status Script using PHP, jQuery and Ajax

  1. I downloaded the code and inserted my own api key in the php file and ran the index.html file. Once i enter the pnr no and press ‘Get PNR Status’ the page only shows ‘Please Wait..’ and doesn’t proceed further. Any idea ?

      1. Actually it was a silly mistake, i forgot to run it in xampp. It’s working fine now. My bad. Thank you very much 🙂

      1. i am working on a project as described below:

        Build an App or Web service to integrate any available map service (such as Google Maps, Bing map, Wiki Map etc) with Indian Rail Enquiry service. The following objectives are to be obtained:

        i) A user can specify a Source place and Destination Place on the map. Your system should display the list of trains between the two places. If the places chosen do not have a train source or destination then use the nearest place that has a rail station. You can directly show the result from http://www.indianrail.gov.in

        ii) Given a train number the route of the train should be shown on the map. The name of the source and destination with the departure and arrival time also to be shown.

        i have done the google map part but stuck on the second part the train number one…………as i dont know know much about scripting and api key usage……….. can you help me with this………..

    1. If you are facing any issue with the API[Railway API], kindly reach them and get it resolved. If any issue with the downloaded script, let me know to resolve it.

  2. Could u explain any other tutorials like seat availability or trains between stations because it has more than one input and more than one responses..

Leave a Reply

Your email address will not be published. Required fields are marked *