简体   繁体   中英

fetch data from db using javascript & php

This js var has the country

<script type="text/javascript">
    var siteCmd_Country="<?php echo siteCmd_Country_P ;?>";  
</script>

function getCountryRedirectUrl() {

        var  url_res = '#';
        if(geoCountryName != '' && geoCountryName != null){

            if(geoCountryName =='United Kingdom'){
                //  window.location=Url+'uktutorsdirectory.co.uk';
                url_res = Url+'UK';//Url+'uk';
            }else if(geoCountryName =='United States'){
                url_res = Url+'usa';
            }else if(geoCountryName =='Canada'){
                url_res = Url+'canada';
            }else if(geoCountryName =='India'){
                url_res = Url+'india';
            }else if(geoCountryName =='Italy'){
                url_res = Url+'italy';
            }else if(geoCountryName =='Ireland'){
                url_res = Url+'ireland';
            }else if(geoCountryName =='Malaysia'){
                url_res = Url+'malaysia';
            }
        }
        return url_res;
}

this function gives the changes the url based on the geoCountryName(It is basically the country frm whr the site has been accessed) What i want is the above method is hardcoded for every country. Now i have created an entry in db which maps country with its aliases. so i pass the geoCountryName to the query and get the aliases and append the url with alias.

url_res = Url+"countryAlias"; which is in JS funct and alias comes frm php-mysql

I tried using php but it is not working fine.below is the code

    $conn = mysql_connect(HOSTNAME, DBUSER, DBPWD);
    mysql_select_db(DBNAME, $conn);
    mysql_query('SET CHARACTER SET utf8'); 
    $sWhere = "CountryName=".siteCmd_Country_P;
    $sql = "SELECT CountryAlias FROM `t_countrycharges`  "
            . "$sWhere ";

    $pacrs = mysql_query($sql, $conn);
    $num_rows = mysql_num_rows($pacrs);
    $row = @mysql_fetch_object($pacrs))
    $CountryAlias = $row->CountryAlias;

Actually the page in which all these code contains is the php page so i need to add all the code in the same page. now when i run the page with these code then the half page doesn't load and it is not showing any output. Please help me with these asap.I need to run the php on same page but there is no action or click event which can be controlled so please assist on it. All help is appreciated.

Thanks, Vinay

You should use ajax to communicate from frontend to backend.

this might be helpful http://www.w3schools.com/ajax/

I'll just paste an example of a project I'm working on here. I think it could help you understand. I assume you're OK with jQuery since you have it as a tag.

To start off, we'll just ajax to get the data. Javascript side:

$.getJSON('urltofilesearch_stores/', {id: locationID}, function (json) {
console.log(json) //this is the data which will be a javascript object
    var html = '';

    for (var i = 0; i < json.length; i++) {
      var obj     = json[i];
      var id      = obj.ID;
      var name    = obj.CountryAlias;

      html += "<div class="result">id:" + id + " name: " + name + "</div>";
    }
  }).fail(function (err) {
    $('.error').html("error in getting the data");
  }).done(function () {
    $('.element').append(html);
  });

And then we have the PHP side. You'll have to echo the results for javascript to be able to see them, and make sure nothing else gets printed.

$sWhere = "WHERE CountryName=".mysql_real_escape_string($_GET['id']);
$sql = "SELECT CountryAlias FROM `t_countrycharges`  "
        . " $sWhere ";
$pacrs = mysql_query($sql, $conn);
$data = array();
while($row = mysql_fetch_assoc($pacrs)){
  $data[]['ID'] = $row['ID'];
  $data[]['CountryAlias'] = $row['CountryAlias'];
}

return json_encode($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM