Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array with a list of states and I am trying to get it to show up in my select list. I am getting no errors, but nothing is showing up in the select list as an option. I am just trying to get it to loop through the array and display the states in the HTML.

function statesList() {
$states = array('AL'=>"Alabama",
                'AK'=>"Alaska",
                'AZ'=>"Arizona",
                'AR'=>"Arkansas",
                'CA'=>"California",
                'WY'=>"Wyoming");
return $states;
}

$email_form = '<?php $states = statesList(); ?>
<form class="aw-contact-form" method="post" action="' . get_permalink() . '">
        <label for="cf_state">' . $label_state . '</label>
            <select name="state" id="cf_state">
                <option selected="selected"></option>
                <?php foreach($states as $key=>$value) { ?>
                <option value="<?php echo $key; ?>"><?php $value; ?></option>
                <?php } ?>
            </select>
</form>';

return $email_form;

Is my syntax wrong? Any help will be greatly appreciated.

share|improve this question
1  
Try echoing $value –  advermark Jul 3 at 5:58
1  
You can't use <?php inside a string, it can only be used when you're outside the PHP script, to get back into PHP execution mode. –  Barmar Jul 3 at 6:05

3 Answers 3

up vote 2 down vote accepted

Yes the syntax has issues, foreach was placed inside the string so it was parsed as a string not php code to execute. Also $states was empty because it was placed outside the function. To get the $states value you had to call the function. This is the fixed version:

function statesList() {
$states = array('AL'=>"Alabama",
                'AK'=>"Alaska",
                'AZ'=>"Arizona",
                'AR'=>"Arkansas",
                'CA'=>"California",
                'WY'=>"Wyoming");
return $states;
}    


$email_form = '<form class="aw-contact-form" method="post" action="' . get_permalink() . '">
        <label for="cf_state">' . $label_state . '</label>
            <select name="state" id="cf_state">
                <option selected="selected"></option>';

$states = statesList();
foreach ($states as $key => $value) {
    $email_form .= '<option value="' . $key . '">' . $value . '</option>';
}
$email_form .= '</select>
</form>';

return $email_form;
share|improve this answer
    
Thanks for everyone for their answers, but this one did the trick. Thanks Syed! –  Justin Jul 3 at 6:13
    
you are welcome :) –  Syed Qarib Jul 3 at 6:14

There was a few syntax issues, but I cleaned them up for you.

<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
function statesList() {
$states = array('AL'=>"Alabama",
                'AK'=>"Alaska",
                'AZ'=>"Arizona",
                'AR'=>"Arkansas",
                'CA'=>"California",
                'WY'=>"Wyoming");
return $states;
}

$email_form = ($states = statesList());
?>
<form class="aw-contact-form" method="post" action="hello.php">
        <label for="cf_state"><?php echo $label_state  ?> </label>
            <select name="state" id="cf_state">
                <option selected="selected"></option>
                <?php foreach($states as $key=>$state) { ?>
                <option value="<?php echo $key; ?>"><?php echo $state; ?></option>
                <?php } ?>
            </select>
</form>
<?php 
return $email_form;
?>
</body>
</html>
share|improve this answer

Try this:

<?php 
error_reporting(0);
function statesList() {
$states = array('AL'=>"Alabama",
                'AK'=>"Alaska",
                'AZ'=>"Arizona",
                'AR'=>"Arkansas",
                'CA'=>"California",
                'WY'=>"Wyoming");
return $states;
}
$states = statesList(); 
$email_form .= '<form class="aw-contact-form" method="post" action="' . get_permalink() . '">
                <label for="cf_state">' . $label_state . '</label>
                <select name="state" id="cf_state">
                <option selected="selected"></option>';
                foreach($states as $key=>$value) { 
                $email_form .= '<option value="'.$key.'">'.$value.'</option>';
                 } 
            $email_form .= '</select>
            </form>';

return $email_form;

?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.