返回html id的输出到php字符串
Return the output of html id to php string
我喜欢做一个基于访问者状态/地区的广告。还可以显示基于访问者国家的语言。
我正试图从这个网站获得数据,并在(仅显示输出)中工作良好:
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
// example where I update content on the page.
jQuery('#city').html(location.city);
jQuery('#region-code').html(location.region_code);
jQuery('#region-name').html(location.region_name);
jQuery('#areacode').html(location.areacode);
jQuery('#ip').html(location.ip);
jQuery('#zipcode').html(location.zipcode);
jQuery('#longitude').html(location.longitude);
jQuery('#latitude').html(location.latitude);
jQuery('#country-name').html(location.country_name);
jQuery('#country-code').html(location.country_code);
}
} );
当然,这将给出浏览器上的访问者状态数据:
<div id="region-name"></div>
问题:
- 我如何得到id输出保存在php字符串。
- 我想让它使用PDO准备语句数据库。
我试图通过以下方式来保存它:
$state='<div id="region-name"></div>';
$pages->testsavestate($state); // save to database PDO
public function testsavestate($state) {
$ses_id = session_id();
$country='mycountry';
$query = $this->db->prepare("INSERT INTO `visitors`(`session`, `country`, `state`) VALUES
(?,?,?)");
$query->bindValue(1, $ses_id);
$query->bindValue(2, $country);
$query->bindValue(3, $state);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
不保存状态结果,只保存标签上面的结果。
谢谢
为了使该数据从您的第一个AJAX调用传递到获取位置值。您还可以在此基础上对PHP处理调用另一个ajax。考虑这个例子:
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
jQuery('#city').html(location.city);
jQuery('#region-code').html(location.region_code);
jQuery('#region-name').html(location.region_name);
jQuery('#areacode').html(location.areacode);
jQuery('#ip').html(location.ip);
jQuery('#zipcode').html(location.zipcode);
jQuery('#longitude').html(location.longitude);
jQuery('#latitude').html(location.latitude);
jQuery('#country-name').html(location.country_name);
jQuery('#country-code').html(location.country_code);
// after your .html() below
// after your successful ajax outside, call your php file
var country = location.country_name;
var state = location.region_name;
// call your php file
jQuery.ajax({
url: 'index.php', // <-- name of the php file that will handle such request
type: 'POST',
dataType: 'JSON',
data: { country: country, state: state },
success: function(response) {
alert(response);
}
});
}
});
然后在php文件中处理
if(isset($_POST['save'])) {
$country = $_POST['country'];
$state = $_POST['state'];
$ses_id = session_id();
$query = $this->db->prepare("INSERT INTO `visitors`(`session`, `country`, `state`) VALUES (?,?,?)");
$query->bindValue(1, $ses_id);
$query->bindValue(2, $country);
$query->bindValue(3, $state);
try {
$query->execute();
if($query->rowCount() > 0) {
echo "Save complete!";
exit;
}
} catch(PDOException $e){
die($e->getMessage());
}
}
相关文章:
- 我们可以将ng绑定值存储到php字符串中吗
- 传递php字符串作为php脚本中调用的javascript函数的参数
- 简单的PHP字符串检查不起作用
- PHP字符串到json数组
- 如何传递PHP字符串作为JS函数参数
- 使用爆炸将php字符串转换为数组,需要在javascript中帮助操作
- PHP 字符串正在将空格更改为 + 符号.需要解码以保留空格
- 将 PHP 字符串传递给 JavaScript 函数
- 简单的php字符串问题(HTML和javascript)
- 来自 php 字符串的动态链接
- Javascript PHP 字符串未返回所需的结果
- 使用 JavaScript 实现 PHP 字符串异或
- 如何放置 PHP 字符串 jQuery 验证错误消息
- 将 php 字符串作为 json 传递,以便由 jquery 接收
- 提取 php 字符串中以 $ 开头的单词
- 将javascript输入转换为php字符串
- PHP 字符串包含打印 Javascript 时的换行符问题
- 在javascript中放一个php字符串
- 我想将这个echo块中php字符串的值大写
- jquery$.parseXML无法从php字符串中解析XML