【发布时间】:2016-08-09 04:50:09
【问题描述】:
我正在尝试从另一个页面获取数据并将其响应和内容加载到我拥有表单的页面中的 div 中 - 无需重新加载表单页面。当我尝试此代码时,它会刷新页面并且不会获取结果。
我的表单所在的当前 jquery 代码 - form.php:
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
<script>
$('#skipreload').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('GET'), // GET or POST
url: $(this).attr('dbresults.php'), // the file to call
success: function(response) { // on success..
$('#form_output').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});
</script>
</head>
<body>
<form id="skipreload" method="GET" action="form.php">
<input type="text" name="id">
<input type="submit" value="Get results">
</form>
<div id="form_output">
<!--Show the result here from dbresults.php-->
</div>
</body>
</html>
我要从中获取结果的页面中的代码 - dbresults.php:
include("dbsettings.php");
$id = "";
if ($_GET)
{
$id = $_GET['id'];
$sql = "SELECT Results FROM Table WHERE id = '$id';";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row["Results"]";
}
}
当我在 form.php 中提交表单时,页面会重新加载,而不会从 getresults.php 将结果放入 div "#form_output" - 为什么它不起作用?
【问题讨论】:
-
查看浏览器的开发者控制台可能会帮助您自己解决这个问题。只是将来要考虑的事情。
标签: javascript php jquery ajax submit