私は PHP にはまったく問題ありませんが、jQuery にはまったく慣れていないため、フォーム データの自動保存に行き詰まりました。
このautosave
関数は で 30 秒ごとに呼び出されdummy.php
ます。シリアル化されたフォーム データを処理用 (--> データベース) に送信していますsavetest.php
。
現時点で、私はこの質問に行き詰まっています:
savetest.php
着信データを「リッスン」してそれに反応するにはどうすればよいですか?
この瞬間、「ああ、いやだ!」というアラートが表示されます。( = 成功なし) 30 秒ごと。
これが私の短縮サンプルコードです:
dummy.php
、スニペット 1 (HTML & PHP):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="../../_include/jquery-1.9.1.min.js"></script>
</head>
<body>
<h1>My Form</h1>
<form name="form1" id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="radio" name="item_2_1" id="c_2_1_0" value="0" />
<input type="radio" name="item_2_1" id="c_2_1_1" value="1" />
<input type="radio" name="item_2_1" id="c_2_1_2" value="2" />
<input type="radio" name="item_2_1" id="c_2_1_3" value="3" />
<input type="checkbox" name="item_3_1[]" id="c_3_1_0" value="yes" />
<input type="checkbox" name="item_3_1[]" id="c_3_1_1" value="no" />
<input type="text" name="item_4_1" id="c_4_1_0" />
</form>
dummy.php
、スニペット 2 (jQuery):
<script type="text/javascript">
function autosave() {
jQuery('form').each(function() {
jQuery.ajax({
url: 'savetest.php',
data: {
'autosave' : true,
'formData' : jQuery(this).serialize()},
type: 'POST',
success: function(data){
if(data && data == 'success') {
alert("OK!");
}else{
alert("Oh no!");
}
}// end successful POST function
}); // end jQuery ajax call
}); // end setting up the autosave on every form on the page
}// end function autosave()
jQuery(function($) {
setInterval(autosave, 30 * 1000);
});
</script>
これはsavetest.php
次のとおりです。
if (isset($_POST)) {
var_dump($_POST);
exit();
} else {
echo 'Hi, no $_POST.';
}
残念ながら、まだ失敗したアラート...とsavetest.php
ダンピングarray(0){}
:-(