Page 2 of 2

Re: PHP IF code

Posted: Mon Aug 11, 2014 1:32 pm
by donny
i used the code for 5.2 and it still doesn't work.

<select name="state" >
<?php
$states = array(
'CA' => 'California',
'FL' => 'Florida',
'NY' => 'New York',
);
?>
<option value="">---</option>
<?php foreach ($states as $abbr => $name): ?>
<option value="<?= $abbr; ?>"><?= $name; ?></option>
<?php endforeach; ?>
</select>
thanks a lot for helping me.. i tried to upgrade to 5.5 in my cpanel but i get internal server error so i reverted back to 5.2

Re: PHP IF code

Posted: Mon Aug 11, 2014 1:35 pm
by donny
there isn't a way i can just set a variable on my process page

$fullstatename = florida
$abbreviation = FL

thats all i want to come out of it

Re: PHP IF code

Posted: Mon Aug 11, 2014 2:10 pm
by Celauran
donny wrote:it still doesn't work.
That's not really giving me much to work with. Is error reporting turned on? What error(s) are you seeing? What's happening? The more specific you can be, the better.

Re: PHP IF code

Posted: Mon Aug 11, 2014 2:29 pm
by donny
how do i turn on error reporting? in my cpanel i have it turned on if thats how it is turned on.

the form option field doesn't list any states

Code: Select all

<select name="state" >
<?php
$states = array(
        'CA' => 'California',
        'FL' => 'Florida',
        'NY' => 'New York',
);
?>
        <option value="">---</option>
        <?php foreach ($states as $abbr => $name): ?>
        <option value="<?= $abbr; ?>"><?= $name; ?></option>
        <?php endforeach; ?>
</select>

Re: PHP IF code

Posted: Mon Aug 11, 2014 2:31 pm
by donny
basically i just want to have my form option select a state... and on my next page i want it to be in 2 variables $fullstatename $stateabbreviation

thats all

Re: PHP IF code

Posted: Mon Aug 11, 2014 2:40 pm
by Celauran

Code: Select all

error_reporting(-1);
You don't really want to be doing that on a live site, mind you. Do you not have some sort of dev/staging environment? Trying to guess what errors you're encountering on a server you don't control running a badly outdated version of PHP is going to be a challenge.

Re: PHP IF code

Posted: Mon Aug 11, 2014 3:17 pm
by donny
my site isn't live yet.. there isnt a way i can set the abbreviation variable on the processing page like if $state = California then $abbreviation = CA

something along those lines?

Re: PHP IF code

Posted: Mon Aug 11, 2014 3:30 pm
by Celauran
That's exactly what we've been trying to do. Using an array seems both the simplest and most easily extensible. Sure, you could set up a bunch of conditionals

Code: Select all

if ($_POST['state'] == 'California') {
    $state_abbr = 'CA';
} else if ($_POST['state'] == 'New York') {
 $state_abbr = 'NY';
}
// etc
But that's not terribly efficient and it's ignoring the larger problem of why iterating over an array isn't working.

Re: PHP IF code

Posted: Mon Aug 11, 2014 4:01 pm
by donny
it works for me.

thanks a lot for helping

Re: PHP IF code

Posted: Mon Aug 11, 2014 5:03 pm
by donny

Code: Select all

if ($_POST['state'] == 'California') {
    $state_abbr = 'CA';
} else if ($_POST['state'] == 'New York') {
 $state_abbr = 'NY';
}
// etc
how can i add more states to this?
also is it possible to run another version of this script with different fields on the same process page?

Re: PHP IF code

Posted: Mon Aug 11, 2014 5:08 pm
by Celauran
Just keep adding else if statements. Like I mentioned above, though, that's really solving the wrong problem.