vascorama Posted August 17, 2014 Share Posted August 17, 2014 (edited) 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 August 17, 2014 by vascorama Quote Link to comment https://forums.phpfreaks.com/topic/290501-need-help-with-javascript-php-mix/ Share on other sites More sharing options...
gristoi Posted August 18, 2014 Share Posted August 18, 2014 you want to get the visitors country code in php, not javascript, you can do this by extracting it from their ip. have a google on "extracting visitors country code from their ip" Quote Link to comment https://forums.phpfreaks.com/topic/290501-need-help-with-javascript-php-mix/#findComment-1488146 Share on other sites More sharing options...
ginerjm Posted August 18, 2014 Share Posted August 18, 2014 If you don't know anything about coding how can we teach you how to get what you need? Quote Link to comment https://forums.phpfreaks.com/topic/290501-need-help-with-javascript-php-mix/#findComment-1488159 Share on other sites More sharing options...
dalecosp Posted August 18, 2014 Share Posted August 18, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/290501-need-help-with-javascript-php-mix/#findComment-1488208 Share on other sites More sharing options...
dalecosp Posted August 18, 2014 Share Posted August 18, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290501-need-help-with-javascript-php-mix/#findComment-1488209 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.