セッションで変数を維持するのに問題があります。ページナビゲーションを通じて同じセッションIDが持続することを確認しPrint_r($_SESSION)
、変数の監視にもを使用しました。
私は4ページを使用しています。
- index.php
- custinfo.php
- custbilling.php
- 確認.php
最初のページでは、フォームを使用して次のページにデータを送信しています。
<form name="prescreen" action="custinfo.php" method="post">
<label>From DIA</label>
<input name="startlocation" id="fromdia" type="radio" value="From DIA">
<label>To DIA</label>
<input name="startlocation" id="todia" type="radio" value="To DIA">
<label>Choose Location:</label>
City:<input name="city" id="city" type="text" />
<span>or</span><br />
Zipcode:<input name="zipcode" id="zipcode" type="text" />
<h3>When do you need picked up?</h3>
<label>Choose Date:</label>
<input name="date" id="date" type="datetime-local" />
<label>Choose Time:</label>
<input name="time" id="time" type="time" />
<input type="submit" value="Get a Ride Now!" class="textbtn"></input>
</form>
custinfo.phpで、ドキュメントの先頭でこれを使用します。
<?php
session_start();
$_SESSION['testvar'] = 'THIS IS A TEST';
$startlocation = $_POST["startlocation"];
$city = $_POST["city"];
$zipcode = $_POST["zipcode"];
$date = $_POST["date"];
$time = $_POST["time"];
//Assign variables to the Session
$_SESSION['startlocation'] = $startlocation;
$_SESSION['city'] = $city;
$_SESSION['zipcode'] = $zipcode;
$_SESSION['date'] = $date;
$_SESSION['time'] = $time;
?>
変数は適切に読み込まれ、配列に格納されます。次に、custinfo.phpページでこのフォームを使用します。
<form name="customerinfo" action="custbilling.php" method="post">
<label>Contact Name</label>
<input name="contactname" id="contactname" type="text">
<label>Contact Email</label>
<input name="contactemail" id="contactemail" type="text">
<label>Contact Phone</label>
<input name="contactphone" id="contactphone" type="text" />
<?php
if($_SESSION['startlocation'] == "From DIA")
{
echo '<hr />';
echo '<label><b>To Location:</b></label>';
echo 'Address1:<input name="toaddress1" id="toaddress1" type="text" />';
echo 'Address2:<input name="toaddress2" id="toaddress2" type="text" />';
echo 'City:<input name="tocity" id="tocity" type="text" />';
echo 'Zip:<input name="tozip" id="tozip" type="text" />';
echo '<hr />';
}
else
{
echo '<hr />';
echo '<label><b>From Location</b></label>';
echo 'Address1:<input name="fromaddress1" id="fromaddress1" type="text" />';
echo 'Address2:<input name="fromaddress2" id="fromaddress2" type="text" />';
echo 'City:<input name="fromcity" id="fromcity" type="text" />';
echo 'Zip:<input name="fromzip" id="fromzip" type="text" />';
echo '<hr />';
}
?>
<input type="submit" value="Book Your Ride!" class="textbtn"></input>
</form>
custbilling.phpページの先頭に、次のようにすべてを取り込みます。
<?php
session_start();
/*Vars from Customer Info */
$contactname = $_POST['contactname'];
$contactemail = $_POST['contactemail'];
$contactphone = $_POST['contactphone'];
if($_SESSION['startlocation'] == "To DIA")
{
$address1 = $_POST['fromaddress1'];
$address2 = $_POST['fromaddress2'];
$city = $_POST['fromcity'];
$zipcode = $_POST['fromzip'];
}
else
{
$address1 = $_POST['toaddress1'];
$address2 = $_POST['toaddress2'];
$city = $_POST['tocity'];
$zipcode =$_POST['tozip'];
}
//Assign Variables to the Session
$_SESSION['contactname'] = $contactname;
$_SESSION['contactemail'] = $contactemail;
$_SESSION['contactphone'] = $contactphone;
$_SESSION['address1']=$address1;
$_SESSION['address2']=$address2;
$_SESSION['city']= $city;
$_SESSION['zipcode'] = $zipcode;
?>
この時点で、次のような情報を表示しています:\
<h1><?php echo $_SESSION['testvar']; ?></h1>
<h2>Travel Information</h2>
<h3>Please fill out this form:</h3>
<p>Direction of Travel: <?php echo $_SESSION['startlocation']; ?></p>
<p>LocationTo: <?php echo $_SESSION['city'] , $_SESSION['zipcode']; ?></p>
<p>Date: <?php echo $_SESSION['date']; ?></p>
<p>Time: <?php echo $_SESSION['time']; ?></p>
<p>Customer Name: <?php echo $_SESSION['contactname']; ?></p>
<p>Customer Email: <?php echo $_SESSION['contactemail']; ?></p>
<p>Customer Phone: <?php echo $_SESSION['contactphone']; ?></p>
<p>Address Information: <br />
<span>Address 1:</span><?php echo $_SESSION['address1']; ?><br />
<span>Address 2:</span><?php echo $_SESSION['address2']; ?><br />
<span>City:</span><?php echo $_SESSION['city']; ?><br />
<span>ZipCode:</span><?php echo $_SESSION['zipcode']; ?><br />
</p>
ただし、配列に保存された最初の投稿の開始位置とすべての変数が消去されるようになりました。また、セッションが機能していることをテストするために使用したtestvariableと、$_SESSION['testvar']
すべてのファイルで正しく表示されることも確認できます。最初はそのようにコードを設定していました$_SESSION['varname] = $_POST['varname'];
が、同じ問題が発生しました。したがって、変数は次のページに移動しますが、3番目のページには進みません。
どんな助けでも大歓迎です。ありがとうございました。
編集:これは有用な情報かもしれません:
Result from custinfo.php : bs2rrqoo5u1u5mjerkg54nkcb1 Array ( [startlocation] => From DIA [city] => Denver [zipcode] => [date] => 11/27/2012 [time] => 09:00 [fromaddress1] => [fromaddress2] => [fromcity] => [fromzip] => [toaddress1] => [toaddress2] => [tocity] => [tozip] => [contactname] => [contactemail] => [contactphone] => [address1] => [address2] => [testvar] => THIS IS A TEST [sameaddress] => 1 [billaddress1] => [billaddress2] => [billcity] => [billzip] => [ccnumber] => [ccexp] => [ccsc] => )
Result from custbilling.php : bs2rrqoo5u1u5mjerkg54nkcb1 Array ( [startlocation] => [city] => Aurora [zipcode] => 80017 [date] => [time] => [fromaddress1] => [fromaddress2] => [fromcity] => [fromzip] => [toaddress1] => [toaddress2] => [tocity] => [tozip] => [contactname] => Elijah Gartin [contactemail] => elijah.gartin@gmail.com [contactphone] => 3038804117 [address1] => 124 Test [address2] => [testvar] => THIS IS A TEST [sameaddress] => 1 [billaddress1] => [billaddress2] => [billcity] => [billzip] => [ccnumber] => [ccexp] => [ccsc] => )