Jump to content

Need help with javascript + php mix


vascorama

Recommended Posts

Hello all, i need your help on this.

I don´t know if it´s possible, and if possible, i don´t know how to do it.

 

I catch visitors country code using javascript in a variable called "country".

 

Then, i need to php include diferent files based on that country code, like:

 

if country == UK

include header_uk.php

 

else if country == US

include header_us.php

 

else if country == BR

include header_br.php

 

else

include header.php

 

 

I don´t know nothing about coding, this is just an example.

I don´t have another way of catching visitors country without javascript.

 

Is this possible?

 

Thanks in advance.

Edited by vascorama
Link to comment
Share on other sites

It would be possible with JavaScript also, if you have some way to render the desired PHP on the server in response to an AJAX call.  Keep in mind that this is a pretty big project for a little bit of ... whatever.  You have to write a PHP "server" that responds to the AJAX calls and a suitable JS that will run in the browser to change the header sometime after the JavaScript that determine what country the user is in.  And *that* is the reason why you really should consider just using PHP on the server-side, but we can't determine your system requirements for you. ;)

The basic JS execution would be something like:

//some JS here
if (country=="UK") {
   var thisHTML = myAjaxCall("http://myhost.com/header.php?country=UK"); // of course you must write the PHP to respond to this.
   // (and the Ajax handler)
   var myDiv = document.getElementById("header"); //whatever the document ID is
   myDiv.innerHTML = thisHTML;
} elseif (country=="US") {
   var thisHTML = myAjaxCall("http://myhost.com/header.php?country=US");
   var myDiv = document.getElementById("header"); //whatever the document ID is
   myDiv.innerHTML = thisHTML;
}
Link to comment
Share on other sites

You would also have to decide what the header area would look like *prior* to the JS running (even if that was only a second or two) ... it would also be what the bots/SE's see, and what any users without JavaScript enabled would see, which is, indeed, Yet Another Reason it should be done in PHP first, and then you can play JavaScript magic if it's still needed.

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.