JQuery和Ajax不起作用
我的select标签将给我玩家的名字,然后在后面运行查询,然后显示结果。我读过一些教程,但找不到答案。我在这里添加我的html和php文件代码JQuery和Ajax不起作用,jquery,ajax,Jquery,Ajax,我的select标签将给我玩家的名字,然后在后面运行查询,然后显示结果。我读过一些教程,但找不到答案。我在这里添加我的html和php文件代码 //html and script player.html file <script> <script type="text/javascript" src="jque`enter code here`ry.js"> $(document).ready(function() { $('#p
//html and script player.html file
<script>
<script type="text/javascript" src="jque`enter code here`ry.js">
$(document).ready(function() {
$('#player').on('change', function() {
var qString = '{name: ' + $('#player option:selected').text() + '}';
$.post('getplayer.php', qString, processResponse);
// $('#resultsGoHere').html(qString);
});
function processResponse(data) {
$('#resultsGoHere').html(data);
}
});
</script>
<form id="myForm">
<select id="player" name="player" onchange="showPlayer(this.value)">
<option value = "None">Choose One</option>
<option value = "Zidane">Zidane</option>
<option value = "Messi">Messi</option>
<option value = "Cristiano Ronaldo">Cristiano Ronaldo</option>
<option value = "Bergkemp">Bergkemp</option>
</select></br></br>
<!--<input type="submit" name="submit" value="Submit" />-->
</form>
//php part getplayer.php file
<?php
$name = $_POST['theDropdown'];
echo "$name";
/*
$db = mysql_connect("localhost","root","root");
mysql_select_db("football");
$select = "SELECT * FROM players";
$result = mysql_query($select) or die(mysql_error());
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Club</th>
<th>Nationality</th>
<th>Date of birth</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<th>" . $row['id'] . "</td>";
echo "<th>" . $row['name'] . "</td>";
echo "<th>" . $row['pos'] . "</td>";
echo "<th>" . $row['club'] . "</td>";
echo "<th>" . $row['nationality'] . "</td>";
echo "<th>" . $row['dob'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($db);
*/
?>
//html和script player.html文件
$(文档).ready(函数(){
$('#player')。在('change',function()上{
var qString='{name:'+$('#播放器选项:选定')。text()+'}';
$.post('getplayer.php',qString,processResponse);
//$('#resultsGoHere').html(qString);
});
函数processResponse(数据){
$('#resultsGoHere').html(数据);
}
});
选一个
齐达内
梅西
克里斯蒂亚诺·罗纳尔多
博格坎普
//php部件getplayer.php文件
您的请求变量是name
,但您可以访问dropdown
// this data object will give you a $_POST['name'] value, not $_POST['theDropdown']
var qString = '{name: ' + $('#player option:selected').text() + '}';
你可以这样做
$(document).ready(function(){
$("#player").change(function(){
var dataStr = $(this).val();
$.ajax({
type='POST',
data = 'name='+dataStr,
url:'process.php',
success:function(data) {
alert(data);
}
});
});
});
所以在process.php页面中
echo $_POST['name'];
// Do Whatever You Want
嗯,这看起来很混乱,你能先修复你的HTML标记吗?以完全不正确的
和
开头。在深入研究JS/PHP之前,我们应该已经掌握了(至少是蓝带)HTML、CSS。记住这一点!在你的PHP回音中:
是不正确的,希望你现在就看到你的问题。
亲爱的上帝,我已经厌倦了这一切。请不要发布一堆带有模糊问题的代码,希望有人能解开它们并让它工作。如果你挣扎了几个小时才让别人帮你修好,你会惊讶地发现,从长远来看,你会学到更多东西。
好像这是唯一的问题……:\在我能“破译”剩下的密码后,我会给你回电话:p