Ajax不能很好地工作

时间:2015-12-10 23:37:36

标签: javascript php ajax database session

Ajax函数在打开文件时不能正常工作:

  • $ _ SESSION变量未定义
  • include('file.php')无法正常工作
  • 索引上的其他变量未定义

但$ _SESSION,include和变量在普通页面上工作! 我认为原因是权限,但我修改了所有文件宽度777权限。

我可以使用直接插入数据(不包括)连接到数据库,在文件打开时,但我会使用include或包含在索引页面上的相同变量with

我发布代码,在没有其他查询或标记的情况下进行简化。 页面很简单(在onKeyUp上搜索用户宽度Ajax =“src_usr('function.php',this.value);”),我有:

1)index.php

<?php
  require('config.php'); //Variables to connect database
  session_start(); 
 ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
    <head>
      <meta http-equiv="content-style-type" content="text/css" />
      <meta http-equiv="imagetoolbar" content="no" />
      <meta name="resource-type" content="document" />
      <meta name="distribution" content="global" />
      <meta name="robots" content="index, follow" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
      <meta http-equiv="Cache-control" content="no-cache" />
      <meta http-equiv="pragma" content="no-cache" />
      <meta http-equiv="Expires" content="-1" />
      <title>Cerca Utenti</title>
      <link rel="stylesheet" type="text/css" href="style.css" />
      <script type="text/javascript" src="script.js"></script>
    </head>
    <body style="background-color: #DFDFDF; color: #000;"> 
    <div id="over_top" style="position: relative; top: 0px;">
     <div id="over_main" style="position: relative;">
      <table id="mainx" align="center"><tr>
       <td>
        <table style="width: 100%; height: 100%;"><tr>
         <td style="vertical-align: top; width: 70%;">
            <div class="pad8">
         <div onclick="showdiv('users', 'updw');" style="cursor: pointer;">Cerca Utente</div>
             <div id="users" style="display: none;">
              <input type="text" id="src_users" name="src_users" class="i_text" style="width: 60%;" value="Search Users" onkeyup="src_usr('function.php', this.value);" onfocus="if (this.value == 'Search Users') this.value = '';" onblur="if(this.value == '') this.value='Search Users';" />
          <div id="res_user" style="position: relative; background: #FFF;"></div>
             </div>
            </div>
         </td>
        </tr></table>
       </td>
      </tr></table>
     </div>
    </div>
 <?php
  mysqli_kill();
  mysqli_close(); 
 ?>
    </body>
    </html>

2)script.js

var xhttp = null;
function src_usr(wch, wht) {
    try
    {
        xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
            xhttp = null;
        }
    }
    if(!xhttp && typeof XMLHttpRequest != "undefined")
    {
        xhttp = new XMLHttpRequest();
    }
    else {
        alert("Your Browser not work width AJAX tecnology!");
    }
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
              document.getElementById("res_user").innerHTML += xhttp.responseText;
        }
    };
    xhttp.open("POST", wch, true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send('rnd=' +Math.random()+ '&user=' +wht); //I have insert Math random for cache free
}

3)function.php

<?php
$user = $_POST['user']; 
$data = $_SESSION['user_id']; // THIS VARIABLE IS UNDEFINED (only on Ajax function, in normale page this variable is defined)
if (!empty($user)) {
    //include('config.php'); IF I INCLUDE FILE TO GET VARIABLES FOR  DATABASE CONNECT, THIS ARE UNDEFINED AND ALL PAGE OPEN NOT WORK EXATLY
    $connect = mysqli_connect('myhost', 'my_user', 'my_password', 'my_database');
    if (!$connect) {
        die($lng['CONN_FAILED'] . mysqli_connect_error());
    }
    mysqli_select_db($connect, 'my_database');
    $query = 'SELECT * FROM user_table';
    $risultato = mysqli_query($connect, $query);
    if ($risultato) {
        if (mysqli_num_rows($result) > 0) {
        while ($riga = mysqli_fetch_assoc($risultato)) {
            $data .= ' ' .$riga['user_nome']. ' ';
            $data .= $riga['user_cognome']. '<br />';
         }
        }
        mysqli_free_result($risultato);
    }
    mysqli_kill();
    mysqli_close();
}
    echo $user. ' = ' .$data. '<br />';
?>

我没有解决方案!

1 个答案:

答案 0 :(得分:0)

在function.php文件的顶部写{{1}}来调用$ _SESSION变量..