Get country code from JavaScript to PHP
Discuss coding issues, and scripts related to PHP and MySQL.
-
JanMolendijk
- Posts: 244
- 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
-
Add id from php variable into javascript code
PHP - MySQL
First post
Dear Coursesweb about mail-function I try another way to complete
Now my problem is to get the referenceid from Url.
<script src=...
Last post
Ow sometimes I`m stupid :D thanks it is working now
-
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 ????
-
custom highlight code
PHP - MySQL
First post
Hello Coursesweb I try to update my highlight code from ini_set
but I would also like to color it with string replacements or something else
For...
Last post
I found the answer Thanks for suporting
<h1 style= font-size:20; >
<?php
ini_set('highlight.html', ' font-size= 20px class=...
-
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
-
Get id from url in javascript
JavaScript - jQuery - Ajax
First post
Last time you learnt me how to add an id from url to a javascript
but by this script it is not working what do I do wrong ????
<?php...
Last post
Maybe there is some mistake in other part of your script.
Check to see if there is any js error in browser console, F12.
What value it shows in...