Reputation: 3086
I have a PHP function to create an address from the data retrieved from the database. I want to use the same feature to make a JavaScript function that will do the same. What it does is take the field and if it is empty it does nothing but if there is data then it will append ", " a comma and space.
$parts = array(
$club['clubAdd1'],
$club['clubAdd2'],
$club['clubCity'],
$club['clubCounty'],
$club['clubPostcode'],
);
$address = array();
foreach ($parts as $part){
if ('' != $part){
$address[] = $part;
}
}
$address = implode(', ', $address);
My current attempt is the function is called everytime the keyup is preformed on the form fields (test purposes only)
function autoAddress(){
var address = "";
var address1 = document.address.address1.value;
var address2 = document.address.address2.value;
var city = document.address.city.value;
var county = document.address.county.value;
var postcode = document.address.postcode.value;
var parts = new array[
address1,
address2,
city,
county,
postcode
];
var testAddress = new array();
foreach(parts as part){
if ('' != part){
testAddress[] = part;
}
}
testAddress = array.join(', ', testAddress);
alert(testAddress);
}
Upvotes: 0
Views: 79
Reputation: 7562
var parts = [
club['clubAdd1'],
club['clubAdd2'],
club['clubCity'],
club['clubCounty'],
club['clubPostcode']
];
var address = [];
for (var i=0; i<=parts.length; i++){
if (parts[i]){
address.push(parts[i]);
}
}
var joined = address.join(', ');
Upvotes: 1