0

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

}

7
  • 4
    JavaScript uses Array.join to do what implode does in PHP. Otherwise, just concatenate strings using + Commented May 29, 2013 at 15:58
  • I'm not 100% sure, but I think your last variable doesn't need a comma. $club['clubPostcode'], Try removing the comma. Commented May 29, 2013 at 16:00
  • What is your attemt so far? I don't see any javascript code Commented May 29, 2013 at 16:02
  • Edited code to show what i have so far. Commented May 29, 2013 at 16:12
  • in foreach(parts as part), part will contain a numeric index of the current entry in parts, you have to use parts[part] to access it. Commented May 29, 2013 at 16:15

1 Answer 1

1
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(', ');
Sign up to request clarification or add additional context in comments.

3 Comments

var joined = address.join(', ', address);? join only uses one argument.
Thanks, works minus one thing. The comma is being appended to the end even if there is no more data. Tired it here but keyup mightnt do alerts in jsfiddle jsfiddle.net/NmSBC
Yeah, edited the if statement, no comma in the end now. It happened because the last value in the array parts was accually "undefined" and it WAS different then '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.