PHP conditionals not working as expected in jQuery mobile.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

PHP conditionals not working as expected in jQuery mobile.

Post by drayarms »

I'm building a site with both jQuery mobile and native HTML5. I send a PHP cookie named "language" from the site to the browser to and assign it a value of either "english" or "spanish" depending on user input. Then I use the function below to output all content on the site in the language chosen

Code: Select all


      <?php

			function translate ($english_txt,$spanish_txt){

				switch ($_COOKIE['language']){

					case "english":

						echo $english_txt;

						break;

					case "spanish":

						echo $spanish_txt;
	
						break;
	
					default:

 						echo $english_txt;

				}//End of switch


			}//End of translate function

		?>
		
Here is example of a paragraph printed on the website using this fucntion

Code: Select all

<p> <?php translate("Happy New Year","Feliz Navidad"); ?> </p>

Everything works just fine on the HTML site. With the jQM site however, only the default case is echoed. But I do know for a fact that the cookie exists (I used the isset funtion to verify that) and I also know for a fact that the value of the cookie is either "spanish" or "english" depending on the user input. Yet I'm puzzled as to why the conditional doesn't work in jQM. Keep in mind that it works in HTML. I also tried using the if conditional thus:

Code: Select all

<?php
if($_COOKIE['language']=="english"){echo "Happy New Year";}
if($_COOKIE['language']=="spanish"){echo "Feliz Navidad";}
?>
to no avail. Anyone can tell me what I'm not doing right?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: PHP conditionals not working as expected in jQuery mobil

Post by twinedev »

You covered the basics to make sure value IS set, now go to the next level of testing, echo out what is in the variable:

Code: Select all

<?php
function translate ($english_txt,$spanish_txt){

	if (!isset($_COOKIE['language'])) { $_COOKIE['language'] = 'not-set'; }
	
	// Temp for testing only... Remove when done:
	echo '[',$_COOKIE['language'],']';
	
	switch ($_COOKIE['language']){
		case "english":
			echo $english_txt;
			break;
		case "spanish":
			echo $spanish_txt;
			break;
		default:
			echo $english_txt;
	}//End of switch
}//End of translate function

?>
This will show you what language the cookie was set to, so you know which case it went through. Note, even though you said you tested it with isset(), in case you did it in another spot, I also added it into the function so that if it wasn't set, you will see it as "not-set" when it hits.

-Greg
Post Reply