2016-07-24 3 views
-1

클래스 할당을위한 패밀리 트리를 작성 중이며 업데이트되는 정보에 따라 해당 패밀리의 HTML 파일에서 PHP 파일로 변수를 전달해야합니다. 나는 아버지, 어머니, 아내, 요법을위한 PHP 파일을 전달할 수 있도록 양식 변수가 필요합니다.html 변수를 여러 개의 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>"; 

?> 

답변

0

그것은이 필요 t 세션은 세션을 사용하여 설정된 데이터에 대한 액세스가 필요한 모든 파일에서 설정/활성화되어야하며 세션 설정은 세션 전역 변수에 저장된 데이터에 액세스해야하는 모든 스크립트에서 가장 먼저해야합니다.

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

다 vid.php FILE :

<?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; 
관련 문제