菲律宾元';在JavaScript HTTP请求下加载第一页上的数据
我正在向PHP发送带有JavaScript HTTP请求的数据,我希望PHP将其保存在内存中。因此,我使用的是菲律宾元';在JavaScript HTTP请求下加载第一页上的数据,javascript,php,session,Javascript,Php,Session,我正在向PHP发送带有JavaScript HTTP请求的数据,我希望PHP将其保存在内存中。因此,我使用的是$\u SESSIONS。我这样做是因为我不想用JavaScript进行任何计算或HTML部署 以下是我向PHP发送数据的方法: /** * The PHP file which receives the data * * @type {string} The php filename */ const INSTALL_FILE = "install.php"; /** *
$\u SESSIONS
。我这样做是因为我不想用JavaScript进行任何计算或HTML部署
以下是我向PHP发送数据的方法:
/**
* The PHP file which receives the data
*
* @type {string} The php filename
*/
const INSTALL_FILE = "install.php";
/**
* Passes roadTaxData to the php install file which could be getData with the $_POST operator
*/
function passToPHP (paramName, data) {
var httpc = new XMLHttpRequest(); // simplified for clarity"
httpc.open("POST", INSTALL_FILE, true); // sending as POST
httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpc.send(paramName + "=" + data);
}
我使用的方法如下:
/**
* @type {string} The data which is getting send to PHP
*/
var data = "";
/**
* Parses the road tax data and stores it into the data string as JSON format
*/
function parseRoadTaxData () {
var object = {};
/*
Loop through all the road tax data
*/
for (var vehicleFormatType in roadTaxData) {
/*
Define all vehicle types which belong to that vehicle type format
*/
var vehicleTypes = roadTaxData[vehicleFormatType];
/*
Loop through all the vehicle types
*/
for (var vehicleType in vehicleTypes) {
/*
Add the vehicle data to the JSON object
*/
object[vehicleType.toLowerCase()] = vehicleTypes[vehicleType];
}
}
/*
Parse the javascript object (converted to JSON format) into the data string.
*/
data = JSON.stringify(object);
}
/**
* Gets the data which is going to be send to PHP
*
* @returns {string} The JSON data string
*/
function getData () {
return data;
}
/*
Call the function to parse all data into the HTTP query string
*/
parseRoadTaxData();
/*
Call the function to pass the whole HTTP query to PHP in seperate $_POST variables
*/
passToPHP('road-tax_data', getData());
/**
* Storage for all road tax data
*/
class RoadTaxDataTemporaryRegistry
{
/**
* The character which gets and sets the specific $_SESSION
*/
const SESSION_NAME = 'rtd';
/**
* Stores all data in a session
*
* @param $data array The data which is getting stored
*/
public static function store ($data) {
$_SESSION[self::SESSION_NAME] = $data;
}
/**
* Gets the road tax data of a vehicle
*
* @param string $vehicle
*
* @return array
*/
public static function getStorageByVehicle ($vehicle) {
return $_SESSION[self::SESSION_NAME]["$vehicle"];
}
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="data/js-files/data_personen_auto.js" type="application/javascript"></script>
<script src="js/road_tax_data_mapper.js" type="application/javascript"></script>
<script src="js/utility.js" type="application/javascript"></script>
<script src="js/data_parser.js" type="application/javascript"></script>
</head>
<body>
<?php
require "RoadTaxDataTemporaryRegistry.php";
session_start();
var_dump(RoadTaxDataTemporaryRegistry::getStorageByVehicle($myCar));
?>
</body>
</html>
接收数据的install.php
文件如下所示:它打开一个会话并将数据存储在RoadTaxDataTemporaryRegistry
类中
require "RoadTaxDataTemporaryRegistry.php";
/*
* Start the session
*/
session_start();
RoadTaxDataTemporaryRegistry::store(json_decode($_POST['road-tax_data'], true));
我的RoadTaxDataTemporaryRegistry
类如下所示:
/**
* @type {string} The data which is getting send to PHP
*/
var data = "";
/**
* Parses the road tax data and stores it into the data string as JSON format
*/
function parseRoadTaxData () {
var object = {};
/*
Loop through all the road tax data
*/
for (var vehicleFormatType in roadTaxData) {
/*
Define all vehicle types which belong to that vehicle type format
*/
var vehicleTypes = roadTaxData[vehicleFormatType];
/*
Loop through all the vehicle types
*/
for (var vehicleType in vehicleTypes) {
/*
Add the vehicle data to the JSON object
*/
object[vehicleType.toLowerCase()] = vehicleTypes[vehicleType];
}
}
/*
Parse the javascript object (converted to JSON format) into the data string.
*/
data = JSON.stringify(object);
}
/**
* Gets the data which is going to be send to PHP
*
* @returns {string} The JSON data string
*/
function getData () {
return data;
}
/*
Call the function to parse all data into the HTTP query string
*/
parseRoadTaxData();
/*
Call the function to pass the whole HTTP query to PHP in seperate $_POST variables
*/
passToPHP('road-tax_data', getData());
/**
* Storage for all road tax data
*/
class RoadTaxDataTemporaryRegistry
{
/**
* The character which gets and sets the specific $_SESSION
*/
const SESSION_NAME = 'rtd';
/**
* Stores all data in a session
*
* @param $data array The data which is getting stored
*/
public static function store ($data) {
$_SESSION[self::SESSION_NAME] = $data;
}
/**
* Gets the road tax data of a vehicle
*
* @param string $vehicle
*
* @return array
*/
public static function getStorageByVehicle ($vehicle) {
return $_SESSION[self::SESSION_NAME]["$vehicle"];
}
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="data/js-files/data_personen_auto.js" type="application/javascript"></script>
<script src="js/road_tax_data_mapper.js" type="application/javascript"></script>
<script src="js/utility.js" type="application/javascript"></script>
<script src="js/data_parser.js" type="application/javascript"></script>
</head>
<body>
<?php
require "RoadTaxDataTemporaryRegistry.php";
session_start();
var_dump(RoadTaxDataTemporaryRegistry::getStorageByVehicle($myCar));
?>
</body>
</html>
这个类除了将变量存储到一个特定的$\u会话
键中之外什么都不做
我的index.php
如下所示:
/**
* @type {string} The data which is getting send to PHP
*/
var data = "";
/**
* Parses the road tax data and stores it into the data string as JSON format
*/
function parseRoadTaxData () {
var object = {};
/*
Loop through all the road tax data
*/
for (var vehicleFormatType in roadTaxData) {
/*
Define all vehicle types which belong to that vehicle type format
*/
var vehicleTypes = roadTaxData[vehicleFormatType];
/*
Loop through all the vehicle types
*/
for (var vehicleType in vehicleTypes) {
/*
Add the vehicle data to the JSON object
*/
object[vehicleType.toLowerCase()] = vehicleTypes[vehicleType];
}
}
/*
Parse the javascript object (converted to JSON format) into the data string.
*/
data = JSON.stringify(object);
}
/**
* Gets the data which is going to be send to PHP
*
* @returns {string} The JSON data string
*/
function getData () {
return data;
}
/*
Call the function to parse all data into the HTTP query string
*/
parseRoadTaxData();
/*
Call the function to pass the whole HTTP query to PHP in seperate $_POST variables
*/
passToPHP('road-tax_data', getData());
/**
* Storage for all road tax data
*/
class RoadTaxDataTemporaryRegistry
{
/**
* The character which gets and sets the specific $_SESSION
*/
const SESSION_NAME = 'rtd';
/**
* Stores all data in a session
*
* @param $data array The data which is getting stored
*/
public static function store ($data) {
$_SESSION[self::SESSION_NAME] = $data;
}
/**
* Gets the road tax data of a vehicle
*
* @param string $vehicle
*
* @return array
*/
public static function getStorageByVehicle ($vehicle) {
return $_SESSION[self::SESSION_NAME]["$vehicle"];
}
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="data/js-files/data_personen_auto.js" type="application/javascript"></script>
<script src="js/road_tax_data_mapper.js" type="application/javascript"></script>
<script src="js/utility.js" type="application/javascript"></script>
<script src="js/data_parser.js" type="application/javascript"></script>
</head>
<body>
<?php
require "RoadTaxDataTemporaryRegistry.php";
session_start();
var_dump(RoadTaxDataTemporaryRegistry::getStorageByVehicle($myCar));
?>
</body>
</html>
问题
如何使页面自动加载数据而不必刷新页面
我自己的尝试
我已将此JavaScript代码放入我的passPHP()
函数中:
httpc.onreadystatechange = function () { //Call a function when the state changes.
if (httpc.readyState == 4 && httpc.status == 200) { // complete and no errors
window.location.href = "index.php";
}
};
这会自动刷新页面,但速度太慢了,实际上您看到的页面上显示数组索引未定义
- 注意:我不想使用jQuery
parseRoadTaxData
onreadystatechange
readystate
的4
并立即进行新的AJAX调用,通过运行RoadTaxDataTemporaryRegistry::getStorageByVehicle($myCar)
function setAndLoadData(INSTALL_FILE, paramName , data)
{
var httpc = new XMLHttpRequest(); // simplified for clarity"
httpc.open("POST", INSTALL_FILE, true); // sending as POST
httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpc.onreadystatechange = retrieveVarsFromPHP //<-- call this on readystatechange.
httpc.send(paramName + "=" + data);
}
function retrieveVarsFromPHP()
{
if (this.readyState == 4)
{
//AJAX call to retrieve data.
var httpc = new XMLHttpRequest(); // simplified for clarity"
httpc.open("get", retrieveSessionData.php, true);
httpc.onreadystatechange = function()
{
if (this.readyState == 4)
{
sessionData = JSON.parse(this.responseText); //Store the data in a global, which can be accessed in the index.html
callBack() //this can be a function in your page called upon to handle displaying the retrieved data.
}
}
httpc.send();
}
}
在index.php上执行
<script>
setAndLoadData();
</script>
setAndLoadData();
检索数据的问题在于AJAX
的asynchronous
部分。当PHP
开始解析数据时,还没有真正设置数据。你需要等待这一切结束。页面的刷新是这方面的一个暗示,但你仍然不能相信这一点。因为页面加载完成这一事实与除了页面加载之外运行的异步AJAX调用已经完成这一事实无关
我希望我说的有道理。我真的不明白你的意思,ajax是jquery唯一正确的方法吗?没有ajax是你正在使用的方法
A
synchronousJ
avascriptA
ndX
ML。在JavaScript中,您使用xmlHttpRequest
实现此目的。只有这种方式随着时间的推移而发展,您还可以通过这种请求加载文本、JSON或XML以外的其他内容。AJAX可以异步发送和接收数据,因此在执行时不会锁定浏览器。好的,但我不知道如何解决这个问题,因为您想要检索数据,但在使用PHP访问数据时不会设置数据。我的脚本等待数据被设置,然后检索数据。但是我在哪里调用它呢?