Ajax函数不完全适用于打开的文件:
- 1、$_Session变量未定义
- 2、include('file.php') 不工作
- 3、索引上的其他变量未定义
但是$_Session、Include和Variable在普通页面上还是工作的!我认为原因在于权限,所以我修改了所有文件宽度777权限。我可以在打开的文件上连接到数据库,直接插入数据(不包含),但是我会使用包含或包含在索引页面中的相同变量。
我的代码没有其他查询或标签,页面非常简单(在onKeyUp=“src_usr(‘function.php’,this.value);”上搜索用户宽度Ajax),我有如下代码:
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 />';
?>
有谁能帮帮我吗?