简单的PHP AJAX脚本
我有2个脚本,submit.php
和display.php
。简单的PHP AJAX脚本
我希望能够加载submit.php
,点击提交按钮&将result
格显示为123
。
现在,它只是重新加载我的网页。我没有从我的控制台得到任何东西,所以坚持如何调试。
有人可以看看并提供帮助吗?
submit.php:
<form>
<input type="submit" value="Submit" name="display" id="display">
</form>
<div id="result"> </div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$("#display").click(function() {
$.ajax({
url: 'display.php',
type: 'GET',
dataType: "html",
data: {
"id": "123",
},
success: function(data) {
//called when successful
$('#result').html(data);
},
error: function(e) {
//called when there is an error
//console.log(e.message);
}
});
});
</script>
Display.php的:
<?php
$id = $_GET['id'];
echo $id;
?>
它提交或重新加载页面。您需要防止默认操作。更改前两行:
$("#display").click(function(e) { // Add e (event) parameter.
e.preventDefault(); // Prevent the default action for this event.
您可能变化type="button"
,但不知道这是否是有效的。
*“您可以更改为type =“button”,但不知道它是否有效。“* - 我确定它会;-) –
它肯定是 - 只是测试它:) – michaelmcgurk
@michaelmcgurk LoL。感谢您的确认。 ':)'我在执行它时遇到了问题。我应该搞点东西或者想念一些东西。 –
解决您的问题,请
replace
<input type="submit" value="Submit" name="display" id="display">
by
<button type="button" name="display" id="display">Submit</button>
变化'类型= “提交”''给类型= “按钮”'。您只需提交表单并重新加载页面。您需要防止提交表单。 – j08691
@ j08691我相信即使那样,它也会重新加载它。 –
@PraveenKumar如果输入不是提交按钮,为什么会重新加载? – j08691