Jump to content

Please help me decipher a simple line of PHP code


DrNick

Recommended Posts

Hi Everybody!

 

I am new to PHP (in fact, I know next to nothing about it). I have been tasked with making a change to the company website. This website was written by somebody else and is written in PHP. The page in question has a form with a content box to put in "State". It is currently storing the full state name and I need to change it to 2 letter abbreviations. The code looks like this:

 

<option value="California"<? if( isset( $_POST[ 'State' ] ) && $_POST[ 'State' ] == 'California' ) { echo ' selected'; } ?>>California</option>

 

I've been able to figure out that the 3rd "California" is  what is listed in the box and that the 1st "California" is what is saved but I'm not sure what this part means:

 

<? if( isset( $_POST[ 'State' ] ) && $_POST[ 'State' ] == 'California' ) { echo ' selected'; } ?>

 

I'm coming up empty using google to decipher it and I don't want the form not to work correctly. Can anybody translate it into plain english for me?

 

Thanks in advance.

 

 

 

Link to comment
Share on other sites

If I'm following you right I think you want something along these lines;

 

<option value="CA"<? if( isset( $_POST[ 'State' ] ) && $_POST[ 'State' ] == 'CA' ) { echo ' selected'; } ?>>California</option>

 

I'm British, but I think CA is for California??

Link to comment
Share on other sites

In english, it's checking data that was already submitted for the existance of a variable named $_POST['state'], which is the isset() bit.  Then, if that variable is set, it wants to compare the value of it against a string that we provide.  That && is a logical AND operator, meaning that both conditions have to be true for the conditional to be run.  So if there's a set variable named $_POST['state'], AND the value of that variable is "California", THEN the phrase " selected" is added, typically to an <option> tag.  Changing it to a two letter abbreviation would simply involve changing all relevant references of California to CA and likely repeating this process with the other states.

Link to comment
Share on other sites

Thanks all. That's everything I needed to know.

 

I want the content box to show "California" and save "CA" to the table so I will change the code to:

 

<option value="CA"<? if( isset( $_POST[ 'State' ] ) && $_POST[ 'State' ] == 'CA' ) { echo ' selected'; } ?>>California</option>

 

Nothing else in the code seems to reference the state name so I don't think the change will break anything else. Thanks again.

Link to comment
Share on other sites

Thanks all. That's everything I needed to know.

 

I want the content box to show "California" and save "CA" to the table so I will change the code to:

 

<option value="CA"<? if( isset( $_POST[ 'State' ] ) && $_POST[ 'State' ] == 'CA' ) { echo ' selected'; } ?>>California</option>

 

Nothing else in the code seems to reference the state name so I don't think the change will break anything else. Thanks again.

 

Just make sure that the action the form's going to will know what to do with this change.  If the submitted form is expecting California but gets CA...see where I'm going with this?  Just as long as everything else is set right it shouldn't be a problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.