Get country code from JavaScript to PHP
Discuss coding issues, and scripts related to PHP and MySQL.
-
JanMolendijk
- Posts: 268
- 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
-
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=...
-
javascript add image to varible
JavaScript - jQuery - Ajax
First post
Hello Coursesweb I do not known mutch about javascipts
& for view hours I gambling to try to get images in this javascript
I try with...
Last post
You had the best support on the internet,
mutch better then stackoverflow.
i learnt alot from you it was really valuable
+ Thanks for all you...
-
How to get text from javascript alert box
JavaScript - jQuery - Ajax
First post
Good evening (dutch-time) I have a javascript what can copy text to a clip-board + it has a button to show the item-list in a alert-box.
But can`t...
Last post
Thanks for the support I tried already things with history + f_print
also studied those a little before I asked coursesweb but I shall
when my head...
-
Dot product of two arrays in Javascript
JavaScript - jQuery - Ajax
First post
What's an efficient way of implementing the dotProduct method (to get the Dot product of two arrays) without importing any new Javascript libraries?...
Last post
Here is a method.
Use the map() function to create a new array with multiplied results of each index and the reduce() function to sum the values of...