2014-01-26 2 views
0

moduleinfo.php에서 managemodule.php로 $ _SESSION 변수로 전달하고 싶습니다. 그러나 내가 통과 시키려 던 모든 변수는 managemodule.php로 전달됩니다.xmlhttp.open()에 지정된 파일에서 PHP 파일로 값을 전달하는 방법은 무엇입니까?

managemodule.php :

//Assume that database connect here 

<form id="moduleform"> 
     <h2 class="fs-title">Module Info Update</h2> 
     <p>Please select a module for more information:</p><br /> 
     <select id="modulelist" name="module" onchange="showModuleInfo(this.value)"> 
      <option>-- Select a module --</option> 
      <?php include('modulelist.php'); ?> 
     </select> 
     <div id="moduleinfo"><br /><b><-- Module info will be listed here --></b></div> 
<?php 
echo $_SESSION['code']; //Just wanted to see if the value get passed 
?> 
    </form> 

managemodule.js :

function showModuleInfo(str) 
{ 
    if (str == "") 
    { 
     document.getElementById("moduleinfo").innerHTML = ""; 
     return; 
    } 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else 
    {// code for IE6, IE5 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      document.getElementById("moduleinfo").innerHTML = xmlhttp.responseText; 
     } 
    } 

    xmlhttp.open("GET","moduleinfo.php?q="+str,true); 
    xmlhttp.send(); 
} 

moduleinfo.php :

//Assume that database connect here 

session_start(); 

$q = $_GET['q']; 
$result = mysql_query("SELECT * FROM module WHERE code = '".$q."'"); 

while($row = mysql_fetch_array($result)) 
{ 
    echo '<input type="text" name="update_moduleCode" placeholder="Module Code" value="'.$row['code'].'" required />'; 
    echo '<input type="text" name="update_moduleTitle" placeholder="Module Name" value="'.$row['name'].'" required />'; 
    echo '<textarea name="update_moduleDescription" rows="14" placeholder="Module Description" required>'.$row['description'].'</textarea><br />'; 
echo '<input type="submit" name="update" class="next action-button" value="Update" />'; 
    echo 'OR '; 
    echo '<button name="delete" class="next action-button" value="Delete Module" onclick="deleteAlert()">'.'Delete Module'.'</button>'; 
    $_SESSION['code'] = $row['code']; //Trying to pass this value over to managemodule.php 
} 

사람이 나를 나는 그것을 달성 할 어떻게 알 수 있습니까? 또한, 내가 궁금한 점은, 내가 managemodule.php 인 메인 파일에서 이미 수행했기 때문에 moduleinfo.php에서 데이터베이스를 다시 연결해야하는 이유는 무엇입니까?

답변

1

moduleinfo.php에서와 마찬가지로 session_start();을 사용하여 managemodule.php에서 세션을 시작해야합니다. 이 파일에서 세션을 시작하면 $_SESSION에 저장 한 변수를 사용할 수 있습니다.

데이터베이스에 연결된 파일을 포함하지 않은 경우. 난 더 이상 PHP 파일에서 데이터베이스를 사용 할 때 나는 보통 내가 모든 난에 사용하기 위하여려고하고 포함 넣어 init.php 파일을 생성

예 :.

init.php
// include DB files 
require_once('path/to/database.php'); 
$db = new Database(); 

// include user manager 
require_once('/path/to/user.php'); 
$user = new UserManager(); 

// start session 
session_start(); 

main.php

require_once('init.php'); 

// all of my code goes below here =) 

_D

+0

감사합니다! 나는 내 파일 중 하나에서'session_start();'를 놓쳤다. 건배 :) – SCC

관련 문제