[英]Passing variable from JavaScript to PHP using AJAX doesn't show anything
I'm trying to send a PHP variable to JavaScript using AJAX and implementing to HTML, but the result doesn't show anything. 我正在尝试使用AJAX将PHP变量发送到JavaScript并实现到HTML,但结果并没有显示任何内容。
My HTML code: 我的HTML代码:
<div class="contain" id="konten_data"
style="margin-top: 20px;"></div>
My JavaScript code: 我的JavaScript代码:
function tampilDepan(id){
$.ajax({
type: 'POST',
url: 'userAction.php',
data: 'action_type=tdepan&id='+id,
success:function(html){
$('#konten_data').html(html);
}
});
}
My PHP code (userAction.php): 我的PHP代码(userAction.php):
($_POST['action_type'] == 'tdepan'){
$link = mysqli_connect("localhost", "root", "", "universitas");
$datas = mysqli_query($link, "SELECT * FROM mahasiswa where user='john' ");
if(!empty($datas)){
while ($datak = mysqli_fetch_assoc($datas)){
echo '<div class="row" style="margin-left: -90px;">
<div class="col-xs-6 col-sm-6 col-md-4">
<img class="img-thumbnail img-responsive"
src="images/test/'.$datak['gmb_batik'].'" alt=""></div>
<div class="col-xs-12 col-md-8">
<div class="content-box-large">
<p>'.$datak['desc_batik'].'</p>
</div>
</div>
</div>
<div class="row" style="margin-left: -90px; margin-top: 10px;">
<div class="col-xs-12 col-sm-6 col-md-4 col-md-offset-3">
<div class="content-box-large">
<h1 >asal <strong>'.$datak['asal_batik'].'</strong></h1>
</div>
</div>
<div class="col-xs-16 col-sm-6 col-md-2 col-md-offset-1">
<img class="img-thumbnail img-responsive"
src="login/admin/files/qrcode/'.$datak['qr_batik'].'"
alt="">
</div>
</div>
<div class="row" style="margin-left: -90px; margin-top: 10px;">
<div class="col-xs-12 col-sm-6 col-md-9 col-md-offset-4">
<div class="content-box-large">
<p>'.$datak['pola_batik'].' </p>
</div>
</div>
</div>';
}
}else {``
echo '<tr><td colspan="5">No user(s) found......</td></tr>';
}`
}
I don't know what is wrong, I hope somebody can help me. 我不知道有什么不对,我希望有人可以帮助我。
Try to change the javascript code to 尝试将javascript代码更改为
function tampilDepan(id){
$.ajax({
type: 'POST',
url: 'userAction.php',
data: {action_type: "tdepan", id: id},
success:function(html){
$('#konten_data').html(html);
}
});
}
As you can see, data is passed as an object instead of a string. 如您所见,数据作为对象而不是字符串传递。
Also, be aware that if no user was found, you are putting a <tr>
inside a <div>
. 另外,请注意,如果找不到用户,则将
<tr>
放在<div>
。
try to change in your ajax call. 尝试更改你的ajax调用。
function tampilDepan(id){ var postData ={"action_type":tdepan,"id":id}; $.ajax({ type: 'POST', url: 'userAction.php', data: postData , success:function(html){ $('#konten_data').html(html); } }); }
try to use the format below in calling ajax: 尝试使用以下格式调用ajax:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: '{ "action_type":"' + tdepan + '", "id":"' + id + '"}',
url: 'userAction.php',
success: function (data) {
conosle.log(data);
},
error: function (error) {
console.log(error);
}
});
You are trying to get your data using konten_data
probably by using jQuery selector $('#konten_data').val();
你试图使用
konten_data
来获取数据,可能是使用jQuery selector $('#konten_data').val();
, but I don't see it in the PHP code as where you are pushing the data to render onto the DOM. ,但我没有在PHP代码中看到它,因为你将数据推送到DOM上。 Try setting the value of
konten_data
by adding an element before the success callback or on DOM render and you should be good from there. 尝试通过在成功回调之前添加元素或在DOM渲染上设置
konten_data
的值,你应该从那里做好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.