JSFiddle - React, Tailwind, and code Playground
HTML
<form action="/checkout/updateDelivery" method="post" accept-charset="utf-8" id="deliveryUpdate" class="floatLeft">
<input type="hidden" name="weight" value="200" />
<input type="text" name="postcode" placeholder="Enter Postcode..." />
<input type="submit" value="Calculate Cost" />
</form>
<form action="/checkout/process" method="post" accept-charset="utf-8" id="deliveryProcessor">
<div id="deliveryOptions" class="floatRight"></div>
</form>
JavaScript
function updateDelivery()
{
if($('deliveryUpdate') == null)
{
return false;
}
// This 'observes' our form submit - sort of like onsubmit
$('deliveryUpdate').observe('submit', function(evt) {
// Stop the actual event from happening
evt.stop();
// This is the url we submit to - change it as needed
var url = '/checkout/updateDelivery';
//This will be the form and div we update
var containerForm = $('deliveryUpdate');
var optionsDisplay = $('deliveryOptions');
// Grab all the info in the form
var form_data = containerForm.serialize();
var form = containerForm.innerHTML;
//Get the postcode and replace the "%20" with a space
postcode = form_data.split("=");
postcode = postcode[2].replace("%20", " ");
//If the postcode isn't valid, show a message and exit
if(postcode.match(/^([A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]? {1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})$/g) == null)
{
alert("Crowston's Tack only delivers to the United Kingdom.\r\nPlease enter a valid UK postcode.");
return;
}
// Here we make the request
new Ajax.Request(url, {
method: 'post',
parameters: form_data,
onCreate: function() {
form = containerForm.innerHTML;
containerForm.update(form+'<img src="/images/loader.gif" alt="loading..." class="loader" />');
},
onSuccess: function(transport) {
containerForm.update(form);
NVPResponse = transport.responseText;
if(NVPResponse !== '[object Array]')
{
NVPResponse = new Array(NVPResponse);
...