あるページから別のページに値を渡すことはできますが、次のように値を渡す必要があります、
ページ1: <form action="Page4.php" method="post" name="login">;
Page4.php <? header("Location: page3.php"); ?>;
Page3.php <?header("Location: page2.php"); ?>;
Page1.phpのテキストフィールドの値をPage2.phpのテキストフィールドに渡す必要があるのですが、フォームが直接Page2にリダイレクトされないため、値を渡すことができません。セッション、フォームのポストメソッド、その他いくつかの方法を試してみましたが、まだ成功していません。
コードや提案など、ご教授いただけると幸いです。
ありがとうございます!
編集する............。
答えがわかりました!ありがとうございます!実は私のケアレスミスで、$_sessionの代わりに$_postを使用していました。
現在、動作中です。
ありがとうございました。
こんな感じで使ってください:
page1.php
<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>
その他のPHPページ:
<?php
session_start();
echo $_SESSION['myValue'];
?>
注意点として、出力、HTML、エコーの前に session_start()
を呼び出す必要があります(空白でも可)。
セッションの値を変更し続けることは可能ですが、使用できるのは最初のページ以降です。つまり、1ページ目で設定した値は、他のページに移動するか、ページを更新するまで使用できません。
変数の設定自体は、いくつかの方法のうちの1つで行うことができます:
$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];
また、潜在的なエラーを得る前に変数が設定されているかどうかを確認したい場合は、次のような使い方をします:
if(!empty($_SESSION['myValue'])
{
echo $_SESSION['myValue'];
}
else
{
echo "Session not set yet.";
}
POST だけで解決 - $_SESSION なし
page1.php
<form action="page2.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
ページ2.php
<?php
// this page outputs the contents of the textarea if posted
$textarea1 = ""; // set var to avoid errors
if(isset($_POST['textarea1'])){
$textarea1 = $_POST['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
SESSIONとPOSTを利用した解決方法
page1.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
$textarea1 = "";
if(isset($_POST['textarea1'])){
$_SESSION['textarea1'] = $_POST['textarea1'];
}
?>
<form action="page1.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
<br /><br />
<a href="page2.php">Go to page2</a>
ページ2.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
// this page outputs the textarea1 from the session IF it exists
$textarea1 = ""; // set var to avoid errors
if(isset($_SESSION['textarea1'])){
$textarea1 = $_SESSION['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
警告!!!- これには検証は含まれていません!!!