I am creating a Package Tracking Form for a courier company.
Here is my html form
JavaScript
x
8
1
<h2>Track Package</h2>
2
3
<form>
4
<label for="trackingno">Tracking No:</label>
5
<input type="tel" id="trackingno" name="trackingno">
6
<input type="submit" value="Submit">
7
</form>
8
Company has provided the API Link
JavaScript
1
2
1
http://portal.activecourier.pk/api/v1/packet/00003711/status
2
When I click this link I get this data
JavaScript
1
2
1
{"packet_id": "0024-00003711", "consignee_name": "Nasir maqbool", "destination": "Lahore", "current_status": {"status": "Assigned to Courier", "datetime": "2020-12-27T17:55:05.414Z", "comment": null}, "statuses": [{"status": "Pickup request sent", "datetime": "2020-12-27T09:55:41.295Z", "comment": null}, {"status": "Booked", "datetime": "2020-12-26T10:13:15.333Z", "comment": null}]}
2
I want to use html form so visitor enters his package tracking # and get his package details
Advertisement
Answer
They usually use jquery to do this
JavaScript
1
18
18
1
$('#submit').click(function() {
2
const response = $('#response');
3
const trackingId = $('#trackingId').val();
4
let html = '';
5
$('#trackingId').val('');
6
response.html('Please Wait');
7
$.get('http://portal.activecourier.pk/api/v1/packet/'+trackingId+'/status', function(data) {
8
html += '<div><strong>Packet Id:</strong> '+data.packet_id+'</div>';
9
html += '<div><strong>Consignee Name:</strong> '+data.consignee_name+'</div>';
10
html += '<div><strong>Current Status:</strong> '+data.current_status.status+' at '+data.current_status.datetime+'</div>';
11
let statuses = data.statuses.map((e) => {
12
return e.status + ' at ' + e.datetime
13
});
14
html += '<div><strong>Statuses:</strong> <ul><li>'+statuses.join('</li><li>')+'</li></ul></div>';
15
16
response.html(html);
17
});
18
});
JavaScript
1
4
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="response"></div>
3
<input type="text" id="trackingId"/>
4
<button type="button" id="submit">Submit</button>