Get country code from JavaScript to PHP
Discuss coding issues, and scripts related to PHP and MySQL.
-
JanMolendijk
- Posts: 199
- Location: Holland Rotterdam
Get country code from JavaScript to PHP
Chief I have a problem to get the countrycode into my sql-statement.
Please help me out....
This script is to detect the countrycode
Code: Select all
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;
function writeMessage() {
if (language.indexOf('en') > -1) document.write('US');
else if (language.indexOf('nl') > -1) document.write('NL');
else document.write('');
}
// End -->
</script>
<script>writeMessage();</script>
I try with this simple php code to detect $country_code1
Code: Select all
<?php
$country_code1 = '<script>writeMessage();</script>';
?>
This $country_code1 i try to get the result into this sql-document
Code: Select all
<?php
// connect to the "tests" database
$conn = new mysqli('127.0.0.1', 'ip_ban', '123456', 'ip_ban');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT `ip_adress`, `country_code` FROM `ip_ban` WHERE ip_adress='$ip' or country_code='$country_code1' LIMIT 1 ";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
while($row = $result->fetch_assoc()) {
echo ' <p> '. $row['ip_adress']. ' '. $row['country_code']. '
Your Banned
<br>' ;
}
}
else {
echo '';
}
$conn->close();
?>
Admin
The JavaScript code cannot be executed in php.
Javascript is executed in browser, after the php finished to output its data.
Also, Javascript it is not suitable to get the user's country code because the country code can be determined by IP but your JS script reads the browser's language.
To get the country code with javascript you need to use ajax and a third party application, or an IP-to-location service (like: //ip-api.io/ or //maxmind.com/ ).
- Try look on the net for: "php get user country code".
Similar Topics
-
Php-Code to view Code
PHP - MySQL
First post
Dear admin hope you do okey ???
I looking for php code like I can do here with option
to show it on my website ???? so i`m also able to see...
Last post
Thanks Chief
You told me once that you do not judge,
but sometimes I wonder what you think
of my knowledge :D ????
-
Php in html code
PHP - MySQL
First post
Hello,
have you any ideea how this work in html without <?php ?> tags?
<div class= modal-title...
Last post
- The form of the code that you posted it can work with a template system, like php Smarty, which must be included and configured in your php script....
-
Problem JS script with php code
PHP - MySQL
First post
I try to add this script to build a energy-low-mode dark-mode
But It only succeed with one item but I would like to have it with all items 6 rows...
Last post
I apologize the script is a bit too big, and it's only 1 item I don't get fixed but I'll leave it that way .... Thanks for the feedback
-
Comment Script - Error Incorrect verification code
Scripts from this website
First post
I use your comment script from:
It worked normal, but now i getting this error:
Error: Incorrect verification code
Last post
Edit:
It is working fine, i added a picture with more data.
-
Changed html code in ssep search script
Scripts from this website
First post
Hello people.
Wow Jan, what a good search you have made. Unfortunatally, something is going wrong, and I hope it is simple to overcome.
I've...
Last post
Oh. I never was aware that when something was named 'templ', it is meant to be 'template'.
But, thank you very much, for pointing this out in such...