PHP IF code
Moderator: General Moderators
PHP IF code
Hello,
I have a form page that inputs data into a database.
I need help writing a code that will enter data in another field of my database depending on what was inputed into the form.
For example, my form field is state. If they enter Florida I want to be able to enter Florida (original form value) and then in another field of my database I want to add FL for abbreviation of the state they chose. I need these to be in 2 separate fields of my database.
Can somebody help me?
Thank you very much!
I have a form page that inputs data into a database.
I need help writing a code that will enter data in another field of my database depending on what was inputed into the form.
For example, my form field is state. If they enter Florida I want to be able to enter Florida (original form value) and then in another field of my database I want to add FL for abbreviation of the state they chose. I need these to be in 2 separate fields of my database.
Can somebody help me?
Thank you very much!
Re: PHP IF code
How are you handling the abbreviations? What if they enter something outside of the US? What if they spell it wrong?
Re: PHP IF code
the form field is a option field. i don't know how I'm going to handle the abbreviations thats what i need to figure out. theres only 5 states that i am using my business is only in the tri-state area.
thank you
thank you
Re: PHP IF code
The simplest solution may be just an array.
You could use key/value to create the select list and lookup the submitted value to get the full name again. Then again, using that approach, I can't think of a reason you'd need to store both.
Code: Select all
$states = [
'FL' => 'Florida',
'GA' => 'Georgia',
];Re: PHP IF code
For the form
and to process the submitted form.
Something along those lines, at any rate.
Code: Select all
<select name="state">
<option value="">---</option>
<?php foreach ($states as $abbr => $name): ?>
<option value="<?= $abbr; ?>"><?= $name; ?></option>
<?php endforeach; ?>
</select>Code: Select all
if (isset($_POST['state'])) {
$state_full_name = $states[$_POST['state']];
}Re: PHP IF code
so say my form field for the states posts data to the next page and lets say the form field for states is $_POST['state'] how can i set the abbreviation variable ?
for example
state selected is california ($state)
form gets submitted
on the next page i want to be able to echo $state = california and $abbreviation = CA
thanks a lot
for example
state selected is california ($state)
form gets submitted
on the next page i want to be able to echo $state = california and $abbreviation = CA
thanks a lot
Re: PHP IF code
array_search()
Or use the array keys as your form values as I posted above.
Or use the array keys as your form values as I posted above.
Re: PHP IF code
Code: Select all
<select name="state" >
<?
$states = [
'NY' => 'NEW YORK',
'FL' => 'FLORIDA',
];
?> <option value="">---</option>
<?php foreach ($states as $abbr => $name): ?>
<option value="<?= $abbr; ?>"><?= $name; ?></option>
<?php endforeach; ?>
</select>
thanks a lot for helping
Re: PHP IF code
I wouldn't define the states array there, no. Put it somewhere that it's going to be accessible and reusable.
Re: PHP IF code
so your saying use include so i can easily edit them?
i have the code set up like i just posted now and the form doesn't list anything under the option field only ---
i have the code set up like i just posted now and the form doesn't list anything under the option field only ---
Re: PHP IF code
What version of PHP? Short array syntax is only supported after 5.4. Also, you're using a short opening tag, which you shouldn't.
should be
Finally, check that you have error reporting turned on.
Code: Select all
<?
$states = [
'NY' => 'NEW YORK',
'FL' => 'FLORIDA',
];
?> Code: Select all
<?php
$states = [
'NY' => 'NEW YORK',
'FL' => 'FLORIDA',
];
?> Re: PHP IF code
Depends on your application architecture. Maybe it makes sense in a global config, maybe it's model-specific.donny wrote:so your saying use include so i can easily edit them?
Re: PHP IF code
I'm using php 5.2
Re: PHP IF code
That hasn't been supported in three years. http://php.net/eol.php
Re: PHP IF code
If you absolutely must use 5.2, you'll need to use the following syntax:
Code: Select all
$states = array(
'CA' => 'California',
'FL' => 'Florida',
'NY' => 'New York',
);