如何使用ajax从php获取select值
我有两个文件,一个是index.php,另一个是get_content.php,不幸的是我无法在get_content.php上显示任何内容,我不知道我的错误在index.php还是get_content.php上 完整代码 点击 index.php 阿贾克斯: PHP:如何使用ajax从php获取select值,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我有两个文件,一个是index.php,另一个是get_content.php,不幸的是我无法在get_content.php上显示任何内容,我不知道我的错误在index.php还是get_content.php上 完整代码 点击 index.php 阿贾克斯: PHP: 为什么get_content.php中没有显示任何内容?很抱歉,这还不是答案。但我认为您应该将jQuery+AJAX代码更改为: $("#id_content").on('change', function() { v
为什么get_content.php中没有显示任何内容?很抱歉,这还不是答案。但我认为您应该将jQuery+AJAX代码更改为:
$("#id_content").on('change', function() {
var id_content = this.value;
$.ajax({
type:"POST",
url:"get_content.php",
data: { id_content: id_content },
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
get_content.php
你的PHP代码输出的选项正确吗?请看我对我的回答Haikal先生的评论。您将了解为什么不使用$data[id\u content]而使用$data['id\u content']。不需要向我学习。我只知道正确的语法。就是这样。哇,你是个聪明人,我需要学习更多的ajax,你能解释一下为什么我不能使用这个->echo$data[content]$data['Id_content']是正确的语法Mr@Haikal.$data['Id_content']是正确的语法$数据[Id_content]语法不正确Mr@Haikal
<select id='id_content' name='id_content'>
<option value='ID Content'>Content</option>
<?php
$sql = "select id_content from content where status = 'setuju'";
$hasil = mysqli_query($konek, $sql);
while($data = mysqli_fetch_assoc($hasil)) {
echo "<option value='$data[id_content]'>$data[content]</option>";
}
?>
</select>
echo $_POST['id_content'];
$("#id_content").on('change', function() {
var id_content = this.value;
$.ajax({
type:"POST",
url:"get_content.php",
data: { id_content: id_content },
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
You have done mistake here `echo "<option value='$data[id_content]'>$data[content]</option>";`
Change to
<option value=<?echo $data['id_content'];?>><?echo $data['content'];?></option>
And one more change was there.. check below..
<select id='id_content' name='id_content'>
<option value='ID Content'>Content</option>
<?php
$sql = "select id_content from content where status = 'setuju'";
$hasil = mysqli_query($konek, $sql);
while($data = mysqli_fetch_assoc($hasil))
{?>
<option value=<?echo $data['id_content'];?>><?echo $data['content'];?></option>
<?}
?>
</select>
<br>
<div id="result_of_ajax">
</div>
$("#id_content").on('change', function() {
var id_content = $('#id_content').val();
$.ajax({
type:"GET",
url:"get_content.php?id_content="+id_content,
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
<?echo $_GET['id_content'];?>