我遇到了问题。我有一些代码,我需要使用ajax来点击特定链接时的结果。它获取按钮的id并使用ajax在数据库中搜索该id。目前我可以发布到下一页,但我需要同时加载结果,以便发布到页面上说如果该页面有确认它将显示在索引页面上。
的index.php
<script type="text/javascript" src="jquery.min.js"></script>
<button class="Stylebut" style="background-color:yellow;" id="500" onClick="reply_click(this.id)">time</button>
<script type="text/javascript">
function reply_click(clicked_id)
{
$.ajax({
url: 'load.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: clicked_id}),
success: function(data){
console.log(data);
alert("hello")
}
});
}
</script>
load.php
<?php
$userAnswer = $_POST['name'];
?>
<p>This text needs to show on the page and anything else that loads up</p>
我需要将数据发送到页面并使用数据加载该页面并使用ajax在第一页上打印。谢谢你的帮助
答案 0 :(得分:0)
对于load.php
上的给定代码,您没有返回JSON,因此you need to remove this line from your ajax request:
dataType:'json', // add json datatype to get json
或者,您需要提供有效的JSON
<?php
echo json_encode($_POST['name']);