在不重新加载当前页面的情况下从单独的页面获取页面内容到 div 表单提交
Get page content from separate page into div on form submit without reloading current page
我正在尝试从另一个页面获取数据并将其响应和内容加载到我拥有表单的页面中的 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” - 为什么不行吗?
如果您想使用当前表单上的值,您的 $.ajax
call. Only use .attr()
中有一些错误,即便如此您也需要 select 通过属性 name 而不是属性 value.
改为:
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: 'GET', // GET or POST
url: 'dbresults.php', // the file to call
success: function(response) { // on success..
$('#form_output').html(response); // update the DIV
}
});
您也可以使用 $.get
完成相同的任务
$.get('dbresults.php', $(this).serialize(), function(response) {
$('#form_output').html(response);
});
此外,您需要将代码包装在 .ready()
块中:
$(document).ready(function() { // alternatively $(function() {
[...]
});
没有属性 GET
,即 method
的值,并且没有属性 'dbresults.php'
,即 action
的值。
此外,您的代码在表单存在之前被调用
尝试:
$(function(){ // wait until documented is ready so element exists
$('#skipreload').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // 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
});
});
我正在尝试从另一个页面获取数据并将其响应和内容加载到我拥有表单的页面中的 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” - 为什么不行吗?
如果您想使用当前表单上的值,您的 $.ajax
call. Only use .attr()
中有一些错误,即便如此您也需要 select 通过属性 name 而不是属性 value.
改为:
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: 'GET', // GET or POST
url: 'dbresults.php', // the file to call
success: function(response) { // on success..
$('#form_output').html(response); // update the DIV
}
});
您也可以使用 $.get
$.get('dbresults.php', $(this).serialize(), function(response) {
$('#form_output').html(response);
});
此外,您需要将代码包装在 .ready()
块中:
$(document).ready(function() { // alternatively $(function() {
[...]
});
没有属性 GET
,即 method
的值,并且没有属性 'dbresults.php'
,即 action
的值。
此外,您的代码在表单存在之前被调用
尝试:
$(function(){ // wait until documented is ready so element exists
$('#skipreload').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // 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
});
});