0

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.

1
  • 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. Commented Jul 3, 2014 at 6:05

3 Answers 3

2

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

1 Comment

Thanks for everyone for their answers, but this one did the trick. Thanks Syed!
1

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>

Comments

1

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;

?>

Comments

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.