1

i have problim in jquery i working in interactive map, in click form href for city i wnat insert the city name or number in sql query .

this link link citys :

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

mysql query:

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

How to make a change when the session to mysql query on click the link below Download Page Navigation with jquery

1
  • 6
    You can't communicate with a database directly from JQuery. You'll need to use an AJAX call to send the data to your PHP to handle the inserts. Someone will probably put more insight into this soon enough. Commented May 18, 2012 at 21:11

1 Answer 1

4

It is not possible to use PHP session directly with jQuery, you need to do an ajax call.

Try this.

Explanation:

  • This will capture the value inside the link, do a post to a PHP file and print the data in "result" div without refreshing the page.

(Don't forget to read my observation at the end of the post)

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);
}

}

?>​

Observation:

  • Try using other way to send the country value to the server.

For example:

if I have:

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

In SQL query I will have:

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

A better way could be:

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

So in my JS I will have to replace var country = $(this).html(); for this:

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

Then in your SQL query you will get this:

SELECT * FROM country WHERE country_name = 'us';

It is more reasonable to use codes and no names (names are just to show the user, for better understanding because then you will have more problems to sanitize the value for using it with your query and also use functions like trim(); to remove spaces and others). If you do that you will have to change your query to find the country by code:

SELECT * FROM country WHERE country_code = 'us';

Hope this helps :-)

Sign up to request clarification or add additional context in comments.

7 Comments

@user1402232 no problem, please don't forget to finish your question by clicking the "check" image if you think this post answers your question. I am just telling you because you are new here :-)
brother oscar , i have error in line 'if($_POST() && isset($_POST['city'])){' and it loaded the page in click the city i have code this 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); }); });' the last code for links citys
Wow, this is confusing, I think you are just applying and removing CSS classes and fadeIn and Out everything. Well, If you are getting an error at that point (line 1), check if in the post you are sending the city, for example: "$.post('process.php', { city:city }, function(data){" that value ($_POST['city']) will go to process.php
You can test also with "$.post('process.php', { city:'Miami' }, function(data){" and "$_POST['city']" will be equals "Miami". I am assuming that you have a problem passing the value to the variable because you can't pass line 1 in process.php (I will be back in some hours :/)
Another idea: I think you have syntax errors, example: "var city = '.city_detail#'+ $(this).attr('city');" there is no valid "attr('city')" atributes are id, value, name, type (like in this case: <input type="text" id="txt" name="txt" value="my value"/>)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.