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>
我需要从mysql数据库获取数据而不使用jquery ajax刷新页面。 我有一个PHP脚本,工作正常。 但是,我的JS似乎遇到了一些问题。 这是jquery脚本。 此外,我在同一页面上使用多个jquerys,如maximage和自定义滚动条。 p>
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);
或
count('#result') .html(html);
});
});
code> pre>
这是我的php p>
&lt; link href =“main2.css”rel =“stylesheet”type =“text / css”/&gt;
&lt; link href =“country.css”rel =“stylesheet”type =“text / css”/ &gt;
&lt;?php
$ q = $ _ GET [“q”];
$ con = mysql_connect('localhost','root','password');
if(!$ con)\ n {
die('无法连接:'。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”&lt ; div&gt;“;
whi le($ row = mysql_fetch_array($ result))
{
echo“&lt; blockquote&gt;”。 $ row ['Review']。“&lt; / blockquote&gt;”;
echo“&lt; p&gt;” 。 $ row ['名称']。 “”。 $ row ['ReasonForPanchakarma']。“&lt; / p&gt;”;
}
echo“&lt; / div&gt;”;
mysql_close($ con);
?&gt;
code>
这是我的HTML p>
&lt; p&gt;选择下面的字段以查看您选择的推荐:&lt; / p&gt; \ n&lt; p&gt; &lt; form name =“Country”&gt;
&lt; select id =“CountryName”onChange =“showCountry(this.value)”&gt;
&lt; option value =“”&gt;选择国家/地区:&lt; / option&gt ;
&lt; option value =“USA”&gt; USA&lt; / option&gt;
&lt; option value =“India”&gt; India&lt; / option&gt;
&lt; option value =“Germany”&gt; Germany&lt; / option&gt; \ n&lt; option value =“Russia”&gt; Russia&lt; / option&gt;
&lt; / select&gt;
&lt; / form&gt;
&lt; / p&gt;
&lt; div id =“result_data”&gt;
&lt; script type = “text / javascript”src =“country2.js”&gt;&lt; / script&gt;
&lt; / div&gt;
&lt; / div&gt;
code> pre>
div>
答
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!
答
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,
答
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);
});
});