別のページからデータを取得しようとしていますが、フォームを再ロードせずにフォームのあるページの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"]";
}
}
I form.phpでフォームを送信すると、getresults.phpのdivに "#form_output"という結果が表示されずにページがリロードされます - なぜ動作しませんか?
:あなたのコードは、フォームの前に呼び出されているほか
は
てみ存在します。将来的に考えるべきこと。 – 4castle