2016-07-24 107 views
-1

Ich baue einen Stammbaum für eine Klassenzuweisung und ich muss Variablen aus einem HTML-Formular in eine PHP-Datei für dieses Familienmitglied übergeben, abhängig davon, wessen Informationen aktualisiert werden. Ich brauche die Formularvariablen, um in die php-Datei für Vater, Mutter, Ehefrau usw. übergehen zu können.HTML-Variable an mehrere PHP-Dateien übergeben

HTML Datei

<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>"; 

?> 

Antwort

0

Es ist notwendig, t Hat die Session in allen Dateien gesetzt/aktiviert, die Zugriff auf die Daten benötigen, die mit $_Session eingestellt wurden, und die Einstellung der Sitzung sollte die allererste Sache in jedem Skript sein, das auf Daten zugreifen muss, die in der Session Global Variable gespeichert sind ....

HTML FILE:

<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 DATEI:

<?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(); 
     } 
    } 

da vid.php Datei:

<?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;