2 Replies - 424 Views - Last Post: 24 December 2012 - 08:06 PM Rate Topic: -----

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 11

PHP conditionals not working as expected in jQuery mobile.

Posted 24 December 2012 - 04:15 PM

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


      <?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

<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:

<?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?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP conditionals not working as expected in jQuery mobile.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: PHP conditionals not working as expected in jQuery mobile.

Posted 24 December 2012 - 06:28 PM

And yet you show no jQuery in your code. Where is it? Remember that PHP is a server side language. It doesn't know whether the request is coming from a desktop, mobile, tablet etc. One thing it does look at is where the cookie is being set.. the domain of the cookie. So if you are setting the cookie on the HTML version of a domain like www.myexample.com and your mobile site is m.myexample.com these are two different domains so it won't find the cookie.

So I am a bit confused as to what you mean by jQuery Mobile is not handling the cookies. Perhaps you can elaborate and show us the jQuery you have that is dealing with cookies. (My guess is that jQuery has nothing to do with it and you are just talking about a mobile version of the site which is probably at a different domain than where you set your cookie).

:)
Was This Post Helpful? 0
  • +
  • -

#3 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 11

Re: PHP conditionals not working as expected in jQuery mobile.

Posted 24 December 2012 - 08:06 PM

@Martyr2 I used the same javacript script to send the input variable to the server via ajax, and that variable is used to determine the value of the cookie. For now, I have just set the name an value of the cookie and no additional parameters. The mobile and main sites are on the same domain www.mysite.com/welcome.php vs www.misite.com/mobile_welcome.php so the domain isn't the issue. Like you suggested, I'm going to include the JS and PHP of the AJAX call that takes the user input. The input is in the form of a select menu.



	$(document).ready(function() {

			
		$("#select_lang").change(function(){//Upon selecting a new language

			//Make a javascript variable out of the selected language

			var selected_language = $(this).val();



			$.ajax({//Send the value of selected language assynchronously to be stored as a cookie in browser
						
				type: "post",

				url: "change_language.php",

				data: {data:selected_language},

				statusCode: {

    					404: function() {

      						alert('Could not contact server.');

    					},

    					500: function() {

      						alert('A server-side error has occurred.');

    					}

  				},


  				error: function() {

    					alert('A problem has occurred.');

  				},

				
				success: function() {

					location.reload();
					
				}


			});//End Ajax request




		});//End change function

 
	});//End document ready method	







	<?php

		//address error handling

		ini_set ('display_errors', 1);
		error_reporting (E_ALL & ~E_NOTICE);

		//Create a PHP variable from the value of the data object

		$selected_language = $_POST['data'];


		//Store that as a cookie

		setcookie("language",$selected_language);


	?>



And if it helps, here's the HTML bit for the mobile version


<div data-role="fieldcontain">

    <fieldset id="lang_fieldset" data-role="controlgroup" data-type ="horizontal">


	<label for ="select_lang"> </label>
        <select name="select_lang" id="select_lang" data-icon ="star">
            <option>Language</option>
            <option value="english">English</option>
            <option value="spansih">Spanish</option>
        </select>
    
        
    </fieldset>

</div>



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1