Jump to content

calling php in javascript


TF338

Recommended Posts

Hi,

 

I'm currently in the process of making my first website. :)

 

The problem is that I'm trying to change the language the website will be in by pressing a button that will call the setlocale() function.

But I can't seem to get it to work. From what I read on the internet you can't use php in javascript because it's serverside. But I can't really think of an alternative way of doing this. Any sugestions?

 

 

This is my code btw for what it's worth.

 

 

 

          
                <form action="SetLangEN()" >
                      <input type="image" src="../images/ENflag.jpg" alt= "EN" width="20px"
                   height="15px"  onclick="SetLangEN()" />   
                    </form>
                   
         
                    </form>
                 </div>
             
             <script type="text/javascript">             
             function SetLangEN() {
                <?php   
                 setlocale(LC_ALL, "en_US");  ?> }
             </script>

Link to comment
Share on other sites

You can't put PHP within a JavaScript function like that. PHP is processed by the web server when you first request a page. The response - the result of the PHP - is the HTML/CSS/JS that's handed back to the client (browser), but doesn't contain any PHP or knowledge of the server. The JavaScript is then executed much later by the browser itself on certain client events, such as onclick of an input.

 

I would go with the cookie idea to retain the user's language, as JS and PHP both have access. Before using the value of the cookie though, I would first check if a GET parameter was sent to overwrite it. If there's no GET parameter or cookie, then I would assume a default.

Link to comment
Share on other sites

Ok I'm trying to work with a cookie now, but i still get an error...

 

this is what gets called when i press the button now, but it says object not found on server.

 

 

			<script type="text/javascript">
		function SetLangNL() { 
			alert("Hallo Wereld!");

			  setcookie("locale", "nl_NL", time()+60*60*24*30, "/");// save a cookie (will expire after 30 days)

			 }  </script> 

Link to comment
Share on other sites

Ok, these are the points you need to understand:

 

1)  You cannot mix PHP and javascript.  You've been told twice already and you're still doing it.  setcookie() is a PHP function, and therefore cannot be inside your javascript.  Ok?  Read it again.  Ok now move on.

 

2)  PHP is a language used to generate strings.  Some of those strings are HTML and javascript, and in that sense you can mix PHP/JS syntax inside your PHP script, but once the JS makes it to the user's browser it cannot execute PHP functions.  This is confusing, I know, but you have to understand that a PHP script runs in the PHP interpreter on your server, and all the output of that script forms an HTML/JavaScript document that it sent, in whole, to your user's browser where it it parsed inside an HTML/JavaScript processing engine that is ignorant of PHP.

 

3)  setlocale() in PHP does nothing for your site text, it will only change the output of built-in functions like date().  The article you linked to specifically mentions that you have to store all your translations in a special array and access them through a templating system.  They use the locale as the key to that array, but that's all.

 

4) 

this is what gets called when i press the button now, but it says object not found on server.
  This cannot be the error.  Always ALWAYS copy and paste errors to us.  You have it right there on your screen.  Copy and paste it.

 

Your solution is to make a page which can change its output language (using a set of language files/tables that you design by hand) based on a global variable like a cookie or the locale.  Then, put a language selector on your website which POSTs or otherwise redirects the user to whatever page they're currently on, but with a new language selected.  Your templating system should handle printing the proper translation.

 

-Dan

Link to comment
Share on other sites

Think of it in spatial terms; code execution is an intangible, spatial area in a computer's memory where OPCODE (operational or machine code - the runtime code of your syntax). Each language has its own 'space'. Generally, one language's space cannot interact, dynamically, with another language's space.

 

In your particular example, there are even more obstacles. Javascript, being a client-side language (executes on the user's computer) and PHP, being a server-side language (executes on the web server), have their respective spaces not only separated by the boundaries of spatial execution in memory, but are also geographically separated (both spaces do not execute on the same computer, in practical example. Using a localhost environment, that wouldn't technically be true, but would be in principle).

Link to comment
Share on other sites

Well I took the easy way out and just redirected the user to a page with ?locale=en_US / nl_NL in the link, everything works fine now.

I did learn some important (and i guess basic)  things regarding the interaction of PHP and JS tho.

Thx for helping me out all

 

(The error I got was error 404 btw. :P)

 

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.