duanfen1992 2014-01-26 04:43
浏览 110
已采纳

如何将值从xmlhttp.open()中指定的文件传递给PHP文件?

I wanted to pass something as a $_SESSION variable from moduleinfo.php to managemodule.php. However every variable that I tried to pass wasnt get passed to managemodule.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
}

Can anyone tell me how do I achieve that? Also, just curious, why do I need to connect my database again in moduleinfo.php since I have already done that in my main file which is managemodule.php?

  • 写回答

1条回答 默认 最新

  • douxun4924 2014-01-26 05:24
    关注

    You need to start a session in managemodule.php using session_start(); same as you did in moduleinfo.php. When you start a session in that file any variables you have saved in $_SESSION will be available there.

    If you have not included a file that has connected to the your database. When i have to used the database in more than one PHP file i usually create a init.php file that i put all of the includes i am going to use in.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况