保存ボタンを押すと、チェック状態をcookieで保存する機能を実装したいと考えています。
<form name="frmOutput" method="get" action="#">
<input name="time[]" type="checkbox" value="morning" id="morning" <?php if (in_array('morning', $times)) {?>checked="checked"<?php } ?> > <label for="morning">朝</label>
<input name="time[]" type="checkbox" value="noon" id="noon" <?php if (in_array('noon', $times)) {?>checked="checked"<?php } ?> ><label for="noon">昼</label>
<input name="time[]" type="checkbox" value="evening" id="evening" <?php if (in_array('evening', $times)) {?>checked="checked"<?php } ?> ><label for="evening" >夜</label>
<input type="submit" name="submit_1" value="保存" onClick="recordTime(); return false;" />
この保存ボタンを押すと、JavaScriptで書いたrecordTimeで、チェックされているcheckboxの値を取得します。
<script src="./jquery.js" type="text/javascript"></script>
<script language="JavaScript">
<!--
function recordTime()
{
var checkList = new Array(document.getElementById('morning'),
document.getElementById('noon'),
document.getElementById('evening'));
var times = new Array();
for (i = 0; i < checkList.length; ++i) {
if (checkList[i].checked) {
times.push(checkList[i].value);
}
}
$.get("./index.php", { "time[]": times, "record": 1 });
}
-->
</script>
値がセットされていればcookieに書き込みを行い、リロードすると保存時のチェック状態が再現されます。
<?php
if (isset($_GET['record'])) {
$times = $_GET['times'];
$tmp = serialize($times);
echo $tmp;
setcookie('times', $tmp, time() + 30 * 60*60*24);
exit;
}
$tmp = $_COOKIE['times'];
$tmp = stripslashes($tmp);
$times = unserialize($tmp);
if ($times === false) {
echo "unserialize failed.";
}
?>
以上のコードなのですが、うまくcookieに保存されません。
これとは別にcookieを削除するボタンも作っており、
この削除ボタンでcookieの有効期限を過去にして削除してから、
動作がおかしくなったように感じていますが、
原因がはっきり分かりません。
知識を持っておられる方がいらっしゃれば、
教えて頂ければ助かります。
よろしくお願いしますm(__)m
A 回答 (1件)
- 最新から表示
- 回答順に表示
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
- ・ゆるやかでぃべーと タイムマシンを破壊すべきか。
- ・歩いた自慢大会
- ・許せない心理テスト
- ・字面がカッコいい英単語
- ・これ何て呼びますか Part2
- ・人生で一番思い出に残ってる靴
- ・ゆるやかでぃべーと すべての高校生はアルバイトをするべきだ。
- ・初めて自分の家と他人の家が違う、と意識した時
- ・単二電池
- ・チョコミントアイス
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
<input type="hidden" >で配列...
-
検索時の選択内容を保持する方法
-
PHP ボタンが押されたら処理を...
-
便利だけどダメなinput nameの...
-
HTMLで前の画面に戻る時、入力...
-
MySQL+PHPでキーワード検索を...
-
チェックボックスでの送信
-
POSTやGETの変数をフォーム無し...
-
PHPで画像の渡しが上手く行きま...
-
$_POSTを連想配列で取得したい!!
-
チェックボックスから複数のval...
-
phpでチェックボックスでの値の...
-
フォームの送信ボタンが押され...
-
PHPのmysqlに格納したデータの...
-
PHPでユーザー情報を入力して簡...
-
画面が更新されてもチェックボ...
-
extract($_POST)
-
セッションにIDを振って値を代入
-
POSTデータのNAME属性をVALUEに...
-
GETやPOSTで値を取り出すとき
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
HTMLで前の画面に戻る時、入力...
-
PHP ボタンが押されたら処理を...
-
<input type="hidden" >で配列...
-
検索時の選択内容を保持する方法
-
フォームへの前回入力値をクリ...
-
フォームでのリセットを使う方法
-
PHPで画像の渡しが上手く行きま...
-
画面が更新されてもチェックボ...
-
フォームボタンを押すたびに数...
-
ラジオボタンをsessionで使いたい
-
テキストボックスに初期値を入れる
-
syntax error, unexpected '}' ...
-
【HTML(PHP?)】複数のフォーム...
-
チェックボックスの必須項目指定
-
HTMLのリンクをクリックするこ...
-
DBの値をチェックボックスに反...
-
複数チェックボックスの入力制...
-
チェックボックスのvalueに変数...
-
input nameで日本語を使うと
-
POSTデータのNAME属性をVALUEに...
おすすめ情報