DrNick Posted December 10, 2008 Share Posted December 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 10, 2008 Share Posted December 10, 2008 That's checking if the $_POST variable called "State" is set and if it equals "California" - if it does echo "selected" into the HTML. Quote Link to comment Share on other sites More sharing options...
gevans Posted December 10, 2008 Share Posted December 10, 2008 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?? Quote Link to comment Share on other sites More sharing options...
bluesoul Posted December 10, 2008 Share Posted December 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 10, 2008 Share Posted December 10, 2008 Changing that one line will "break" the rest that depends on the value - you'd have to change the parts of the site that check for "California" and change them all to "CA". Quote Link to comment Share on other sites More sharing options...
DrNick Posted December 10, 2008 Author Share Posted December 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 10, 2008 Share Posted December 10, 2008 You could actually change it to this: <option value="CA"<?php if ($_POST[ 'State' ] == 'CA') {echo ' selected';} ?>>California</option> Quote Link to comment Share on other sites More sharing options...
bluesoul Posted December 10, 2008 Share Posted December 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.