我正在为类赋值构建一个家谱,我必须根据更新的信息将变量从HTML表单传递到该族成员的PHP文件。我需要能够传递给php文件的形式变量的父亲,母亲,妻子,等等。
HTML文件
<form action="handle_family.php" method="post">
<p>Family Member: <select name="name">
<option value="david">David</option>
<option value="linda">Linda</option>
<option value="cayla">Cayla</option>
<option value="sophie">Sophie</option>
<option value="sawyer">Sawyer</option>
</select></p>
<p>Relationship: <select name="relationship">
<option value="father">Father</option>
<option value="mother">Mother</option>
<option value="wife">Wife</option>
<option value="son">Son</option>
<option value="daughter">Daughter</option>
</select></p>
<p>Interests: <input type="text" name="interests" size="60" /></p>
<p>History: <input type="text" name="history" size="60" /></p>
<p>Occupation: <input type="text" name="occupation" size="60" /></p>
<input type="submit" name="submit" value="Update Page" />
</form>
</div>
handle_family.php
<?php // Script 6.2 - handle_reg.php
ini_set ('display_errors', 1);
error_reporting (E_ALL | E_STRICT);
$okay= TRUE;
$relationship= $_POST['relationship'];
$interests= $_POST['interests'];
$history= $_POST['history'];
$occupation= $_POST['occupation'];
$name= $_POST['name'];
if($name == 'david')
{
session_start();
$_SESSION[$father_relationship] = $relationship;
$_SESSION[$father_interests] = $interests;
$_SESSION[$father_occupation] = $occupation;
$_SESSION[$father_name] = $name;
$_SESSION[$father_history] = $history;
include 'david.php';
exit();
}
david.php
<?php // david.php
// Define Variables
$father_name = $_SESSION[$father_name];
$father_relationship = $_SESSION[$father_relationship];
$father_interests = $_SESSION[$father_interests];
$father_history = $_SESSION[$father_history];
$father_occupation = $_SESSION[$father_occupation];
//print father's information
print"<h3>Relationship to Chris</h3>
<p>$father_relationship</p>
<h3>History</h3>
<p>$father_history</p>
<h3>Occupation</h3>
<p>$father_occupation</p>
<h3>Interests</h3>
<p>$father_interests</p>";
?>
有必要在所有需要访问使用$_Session
设置的数据的文件中设置/激活会话,并且在任何需要访问存储在会话全局变量中的数据的脚本上,设置会话应该是第一件事。。。。
HTML文件:
<div>
<form action="handle_family.php" method="post">
<p>Family Member: <select name="name">
<option value="david">David</option>
<option value="linda">Linda</option>
<option value="cayla">Cayla</option>
<option value="sophie">Sophie</option>
<option value="sawyer">Sawyer</option>
</select></p>
<p>Relationship: <select name="relationship">
<option value="father">Father</option>
<option value="mother">Mother</option>
<option value="wife">Wife</option>
<option value="son">Son</option>
<option value="daughter">Daughter</option>
</select></p>
<p>Interests: <input type="text" name="interests" size="60" /></p>
<p>History: <input type="text" name="history" size="60" /></p>
<p>Occupation: <input type="text" name="occupation" size="60" /></p>
<input type="submit" name="submit" value="Update Page" />
</form>
</div>
handle_family.php文件:
<?php // NOTICE THAT THERE IS NOT WHITE-SPACE BEFORE <?php
// Script 6.2 - handle_reg.php
// FILE-NAME: handle_reg.php WHERE YOU HAVE TO SET THE SESSION VARIABLE
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
if(!isset($_SESSION['familyTree'])){
$_SESSION['familyTree'] = array();
}
if(isset($_POST['submit'])) {
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
$okay = TRUE;
$relationship = htmlspecialchars(trim($_POST['relationship']));
$interests = htmlspecialchars(trim($_POST['interests']));
$history = htmlspecialchars(trim($_POST['history']));
$occupation = htmlspecialchars(trim($_POST['occupation']));
$name = htmlspecialchars(trim($_POST['name']));
// STORE EACH NAME IN THE SESSION USING THE LOWER-CASED FATHERS-NAME AS A UNIQUE KEY
$lcName = strtolower($name);
$_SESSION['familyTree'][$lcName]['father_name'] = $name;
$_SESSION['familyTree'][$lcName]['father_history'] = $history;
$_SESSION['familyTree'][$lcName]['father_interests'] = $interests;
$_SESSION['familyTree'][$lcName]['father_occupation'] = $occupation;
$_SESSION['familyTree'][$lcName]['father_relationship'] = $relationship;
if ($lcName == 'david') {
include 'david.php';
exit();
}
}
david.php文件:
<?php // NOTICE THAT THERE IS NOT WHITE-SPACE BEFORE <?php
// FILE-NAME: david.php
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
$name = "david"; // MAKE SURE THIS IS LOWER-CASE...
$fatherInfo = "";
if(!isset($_SESSION['familyTree'][$name])) {
// Define Variables
$father_name = $_SESSION['familyTree'][$name]['father_name'];
$father_history = $_SESSION['familyTree'][$name]['father_history'];
$father_interests = $_SESSION['familyTree'][$name]['father_interests'];
$father_occupation = $_SESSION['familyTree'][$name]['father_occupation'];
$father_relationship = $_SESSION['familyTree'][$name]['father_relationship'];
//print father's information
$fatherInfo = "<h3>Relationship to Chris</h3>" . PHP_EOL;
$fatherInfo.= "<p>$father_relationship</p>" . PHP_EOL;
$fatherInfo.= " <h3>History</h3>" . PHP_EOL;
$fatherInfo.= "<p>$father_history</p>" . PHP_EOL;
$fatherInfo.= " <h3>Occupation</h3>" . PHP_EOL;
$fatherInfo.= "<p>$father_occupation</p>" . PHP_EOL;
$fatherInfo.= " <h3>Interests</h3>" . PHP_EOL;
$fatherInfo.= "<p>$father_interests</p>" . PHP_EOL;
}
echo $fatherInfo;