功能的Ajax不是在文件打開工作完全:阿賈克斯不行exatly
- $ _SESSION變量是不確定的
- 包括( 'file.php')不是指數工作
- 其他變量是未定義
但$ _SESSION,包含和變量在普通頁面上工作! 我認爲原因是許可,但我已修改所有文件寬度777權限。
我可以連接到數據庫,在文件打開,直接插入數據(不包括),但我會用包括或包括索引頁與包括
我張貼代碼,semplify沒有其他上相同的變量查詢或標籤。 的頁面是簡單得多(搜索用戶寬度的Ajax上的onkeyup = 「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 />';
?>
我沒有辦法!在function.php文件的頂部
你必須做的'在session_start(); '也在'function.php'中(如果會話尚未在其他地方啓動)。簡而言之,您必須在需要它的任何文件中啓動會話(並且包含的任何其他文件將從此文件中獲取) –
謝謝! ......只有一個問題沒有解決:爲什麼要包含文件,而不是寫入文件? (其他變量不可能讀取,如果不是$ _SESSION或$ _COOKIE;它是正確的?) – Igr