【问题标题】:how Start PHP Session when click with jquery使用 jquery 单击时如何启动 PHP 会话
【发布时间】:2012-05-26 10:48:26
【问题描述】:

我在 jquery 中有问题 我在交互式地图中工作,在城市的单击表单中我想在 sql 查询中插入城市名称或数字。

这个链接 链接城市:

<a href='#' class='city_pr' id=aden> </a>

mysql查询:

$sql="select * from project  where    city='$_SESSION[CITY]' AND active =1 ";

当会话到 mysql 查询点击下面的链接时如何更改使用 jquery 下载页面导航

【问题讨论】:

  • 您不能直接从 JQuery 与数据库通信。您需要使用 AJAX 调用将数据发送到您的 PHP 以处理插入。很快就会有人对此提出更多的见解。

标签: php jquery mysql session


【解决方案1】:

不能直接用 jQuery 使用 PHP 会话,你需要做一个 ajax 调用。

试试这个。

说明:

  • 这将捕获链接中的值,发布到 PHP 文件并在“结果”div 中打印数据而不刷新页面。

(别忘了在文章末尾阅读我的观察)

HTML:

<a href='#' id='country'>USA</a>
<br/>
<div id="result"></div>

JS:

​$('#country').click(function(){
    // get the value inside the <a> tag
    var country = $(this).html();
    $.post('process.php', { country:country }, function(data){
        // Everything is Ok, so data won't be 0
        if(data != 0){
           // Print country information
           $('#result').html(data);
        }
    });
});

process.php

<?php

if($_POST() && isset($_POST['country'])){

/* Connect to DB */
$link = mysql_connect('server', 'user', 'pwd');
if (!$link) {
    // No connection
    print(0);
    exit();
}

$db = mysql_select_db('db', $link);
if (!$db) {
    // DB selection error
    print(0);
    exit();
}

/* sanitize the value */
$country = mysql_real_escape_string($_POST['country']);

/* do your query */
$sql = "SELECT * FROM country WHERE country_name = '$country'";
$result = mysql_query($sql);

if(mysql_num_rows($result) > 0){
   while($row = mysql_fetch_array($result)){
      // At this point I am supposing that you stored in your table
      // latitudes and longitudes of countries.
      echo "Latitude is: ".$row['latitude']." Longitude is: ".$row['longitude'];
   }
} else {
  // No results found
  print(0);
}

}

?>​

观察:

  • 尝试使用其他方式将国家值发送到服务器。

例如:

如果我有:

&lt;a href='#' id='country'&gt;United States of America&lt;/a&gt;

在 SQL 查询中,我将有:

SELECT * FROM country WHERE country_name = 'United States of America';

更好的方法可能是:

<a href='#' id='us'>United States of America</a>

所以在我的 JS 中,我必须为此替换 var country = $(this).html();

 //outputs 'us'
 var country = $(this).attr('id');

然后在你的 SQL 查询中你会得到这个:

SELECT * FROM country WHERE country_name = 'us';

使用代码而不使用名称更合理(名称只是为了向用户展示,以便更好地理解,因为这样您将有更多的问题来清理在查询中使用它的值以及使用像 trim(); 这样的函数删除空格和其他)。如果这样做,您将不得不更改查询以通过代码查找国家/地区:

SELECT * FROM country WHERE country_code = 'us';

希望这会有所帮助:-)

【讨论】:

  • @user1402232 没问题,如果您认为这篇文章回答了您的问题,请不要忘记通过单击“检查”图像来完成您的问题。我只是告诉你,因为你是新来的:-)
  • 兄弟 oscar ,我在 'if($_POST() && isset($_POST['city'])){' 行有错误,它在单击城市时加载了页面我有这个代码jquery : ' $('a.dot').click(function (){ $('a.dot').removeClass('selected'); $(this).addClass('selected'); var city = ' .city_detail#'+ $(this).attr('city'); var htmlCode = $(city).html(); $('.detail_container').fadeOut(500,function(){ $('.detail_container .city_detail').html(htmlCode); $('.detail_container').fadeIn(500); }); });'链接城市的最后一个代码
  • 哇,这令人困惑,我认为您只是在应用和删除 CSS 类以及淡入和淡出所有内容。好吧,如果您在该点(第 1 行)收到错误,请检查您是否在帖子中发送城市,例如:“$.post('process.php', { city:city }, function(data ){" 该值 ($_POST['city']) 将转到 process.php
  • 您也可以使用 "$.post('process.php', { city:'Miami' }, function(data){" 和 "$_POST['city']" 进行测试等于“迈阿密”。我假设您在将值传递给变量时遇到问题,因为您无法在 process.php 中传递第 1 行(我会在几个小时后回来:/)
  • 另一个想法:我认为你有语法错误,例如:“var city = '.city_detail#'+ $(this).attr('city');”没有有效的“attr('city')”属性是 id、value、name、type (例如在这种情况下:)
猜你喜欢
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多