doumei1955
doumei1955
2013-03-19 07:46

Jquery Ajax用于从php mysql获取数据而不刷新页面 - 语法错误[关闭]

已采纳

I need to get data from mysql database without refreshing the page using jquery ajax. I have a php script which is working fine. However, my JS seems to be having some problem. Here is the jquery script. Also, I am using multiple jquerys on same page like maximage and custom scrollbar.

var count = jQuery.noConflict();

count('#CountryName').on ('change',function(){ 
var Country = count('#CountryName').val();
count.ajax({ 
  type: 'GET' ,
  url: 'getcountry2.php', 
  data: 'q=', 
}).done(function( html ) { 
 count('#result').append(html); 
 or 

  count('#result').html(html); 
});
});

Here is my php

<link href="main2.css" rel="stylesheet" type="text/css" />
<link href="country.css" rel="stylesheet" type="text/css" />
<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', 'password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$user = mysql_real_escape_string($user);
$pwd = mysql_real_escape_string($pwd);

mysql_select_db("mahc", $con);

$sql="SELECT * FROM texttesti WHERE Country = '".$q."'";

$result = mysql_query($sql);

echo "<div>";

    while($row = mysql_fetch_array($result))
      {

    echo "<blockquote>". $row['Review'] ."</blockquote>";

    echo "<p>" . $row['Name'] . " " . $row['ReasonForPanchakarma']."</p>";
    }
echo "</div>";

mysql_close($con);
?>

Here is my HTML

<p>Select the Fields below to see the testimonials of your choice:</p>
   <p>  <form name="Country">
 <select id="CountryName" onChange="showCountry(this.value)">
<option value="">Select a Country:</option>
<option value="USA">USA</option>
<option value="India">India</option>
<option value="Germany">Germany</option>
<option value="Russia">Russia</option>
</select>
</form>
</p>
<div id="result_data">
<script type="text/javascript" src="country2.js"></script>
</div>
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongzhuan1185 dongzhuan1185 8年前

    Use simple ajax as:

      $.ajax({
       url:'getcountry2.php',
       datatype:"application/json",
       type:'get',
       data: 'q='+Country, 
       success:function(data){
          count('#result').append(html); 
       },
       error:function(){
          // code for error
       }
     });
    

    hopw it will help!

    点赞 评论 复制链接分享
  • dqd22496 dqd22496 8年前

    Here you go..Country is missing

    var count = jQuery.noConflict();
    
        count('#CountryName').on ('change',function(){ 
        var Country = count('#CountryName').val();
        count.ajax({ 
          type: 'GET' ,
          url: 'getcountry2.php', 
          data: 'q='+Country, 
        }).done(function( html ) { 
         count('#result').append(html); 
         or 
    
          count('#result').html(html); 
        });
        });
    
    点赞 评论 复制链接分享
  • douwaz34842 douwaz34842 8年前

    You didn't pass country value to PHP but you are getting the value in PHP. So it could be a problem. Try this,

    count.ajax({ 
      type: 'GET' ,
      url: 'getcountry2.php', 
      data: 'q='+Country, 
    
    点赞 评论 复制链接分享

相关推荐