0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

51歳(現52)からのプログラミング 備忘 formから配列を送ってクッキーに登録してリダイレクト

0
Last updated at Posted at 2020-06-19

実用的なコードは、コメント欄にご投稿頂きました rfc828様 の素敵なコードをご参照ください(←おすすめ)

構成
sam1.php
sam2.php

sam1.php
<form action="sam2.php" method="post">
<input type="text" name="sam[]" value="<?= $_COOKIE['sam'][0] ?? 'no';?>">
<input type="text" name="sam[]" value="<?= $_COOKIE['sam'][1] ?? 'no';?>">
<button>send</button>
</form>

= $_COOKIE['sam'][int] ?? 'no';?>
条件演算子のNULL合体演算子を使ってます。
条件A ?? 条件B // 条件Aがnullでなければ条件Aを実行、nullなら条件Bを実行

これで、クッキーにsam配列が登録されていれば、input欄に該当する値をセットだね。

sam2.php
<?php
$i=0;
foreach($_POST['sam'] as $v){
  setcookie("sam[$i]",$v);
  $i++;
}
header('Location:sam1.php');

POSTされた配列を、foreachで取り出して、クッキーにsam[]として登録ですね。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?