PNR Status Script using PHP, jQuery and Ajax

PNR Status script featured image

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

<div>
<figure><a href="http://pnr.ngdeveloper.com" target="_blank" rel="noopener"><img class="alignright size-full wp-image-2470" src="http://www.ngdeveloper.com/wp-content/uploads/2014/11/demo1.png" alt="demo" width="181" height="63"></a></figure><div style="float: right;"></div>
<figure><a href="http://ngdeveloper.com/subscriptions/index.php?postDwnId=4" target="_blank" rel="noopener"><img class="alignright wp-image-2473 size-full" src="http://www.ngdeveloper.com/wp-content/uploads/2014/11/download1.png" alt="download" width="181" height="63"></a></figure><div style="float: right;"></div>
</div>

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

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

Requirement:

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

Railway API :

REST API ENDPOINT:
http://api.railwayapi.com/pnr_status/pnr/<pnr_no>/apikey/ENTER_YOUR_API_KEY_HERE/
REST API ENDPOINT with sample PNR and API KEY:

Your final railwayapi will look similar to this:

http://api.railwayapi.com/pnr_status/pnr/4728629631/apikey/7pqu19289191/
  • 7pqu19289191 – Railway API key and
  • 4728629631 – 10 digit PNR number

Required Imports:

jQuery.js

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

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

HTML for PNR 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:

This is where we have main business logic to pass the 10digit PNR Number through AJAX call to the Railway API and get the PNR status without refreshing the page.

AJAX call script to the End point:
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();
}
});
}
}
Validation script:

This below script accepts only the numbers and on enter to starts fetching 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>

Styles to make the page more neat:

<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>

Output:

PNR Status Script Demo

18 comments

Leave a Reply