JavaScript HTTP 요청을 사용하여 데이터를 PHP로 보내고 있습니다. PHP가 메모리에 유지하도록합니다. 그러므로, 나는 $_SESSIONS
님의 것을 사용하고 있습니다. JavaScript로 계산이나 HTML 배치를하고 싶지 않기 때문에이 작업을 수행하고 있습니다.PHP가 JavaScript HTTP 요청의 첫 번째 페이지로드시 데이터를로드하지 않습니다.
/**
* 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());
install.php
파일, 데이터의 모양 수신
여기에 내가 어떻게 PHP로 데이터를 전송하고있어 내 방법입니다 this : 세션을 열고 데이터를 RoadTaxDataTemporaryRegistry
클래스에 저장합니다.
require "RoadTaxDataTemporaryRegistry.php";
/*
* Start the session
*/
session_start();
RoadTaxDataTemporaryRegistry::store(json_decode($_POST['road-tax_data'], true));
내 RoadTaxDataTemporaryRegistry
클래스는 다음과 같습니다
/**
* 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"];
}
}
이 클래스는 특정 $_SESSION
키에 바로 저장하는 변수 후 아무것도하지 않습니다.
내 index.php
은 다음과 같습니다
<!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>
내가 먼저 PHP로 데이터를 전송하는 모든 자바 스크립트 스크립트를 가져옵니다.
내 html 페이지의 본문에서 세션을 열고 호출하고 해당 클래스에서 HTTP 요청을 통해 구문 분석 한 데이터를 인쇄합니다.
문제
나는 이것이 내가이 오류 메시지가 도착 (내 IDE의 PhpStorm 통해 개방) 브라우저에서 처음 열 때 :
공지 사항 : 정의되지 않은 인덱스 : C에서 RTD : \ 사용자를 . 내가 페이지를 새로 고치면 \ 바스 \ 자동차 \ RoadTaxDataTemporaryRegistry.php는 라인 (30) NULL에
는, 데이터가 인쇄지고 어떻게해야 :
array(1) { ["noord_holland"]=> array(46) { [0]=> string(14) "1#26#90#26#101" [1]=> string(17) "551#34#109#34#124" [2]=> string(17) "651#42#129#42#147" [3]=> string(17) "751#55#154#55#175" [4]=> string(17) "851#73#189#87#203" [5]=> string(18) "951#94#223#123#238" [6]=> string(20) "1051#115#257#159#273" [7]=> string(20) "1151#137#290#195#309" [8]=> string(20) "1251#158#324#230#344" [9]=> string(20) "1351#180#358#266#379" [10]=> string(20) "1451#201#392#302#414" [11]=> string(20) "1551#222#426#338#449" [12]=> string(20) "1651#244#460#374#485" [13]=> string(20) "1751#265#494#409#520" [14]=> string(20) "1851#286#528#445#555" [15]=> string(20) "1951#308#562#481#590" [16]=> string(20) "2051#329#596#517#625" [17]=> string(20) "2151#351#630#553#661" [18]=> string(20) "2251#372#664#588#696" [19]=> string(20) "2351#393#698#624#731" [20]=> string(20) "2451#415#732#660#766" [21]=> string(20) "2551#436#766#696#801" [22]=> string(20) "2651#458#800#732#837" [23]=> string(20) "2751#479#834#768#872" [24]=> string(20) "2851#500#868#803#907" [25]=> string(20) "2951#522#902#839#942" [26]=> string(20) "3051#543#936#875#977" [27]=> string(21) "3151#564#969#911#1013" [28]=> string(22) "3251#582#1000#943#1044" [29]=> string(22) "3351#600#1030#975#1075" [30]=> string(23) "3451#617#1060#1006#1106" [31]=> string(23) "3551#634#1089#1038#1138" [32]=> string(23) "3651#651#1119#1070#1169" [33]=> string(23) "3751#669#1149#1102#1200" [34]=> string(23) "3851#686#1179#1133#1231" [35]=> string(23) "3951#703#1209#1165#1262" [36]=> string(23) "4051#720#1239#1197#1293" [37]=> string(23) "4151#738#1269#1228#1324" [38]=> string(23) "4251#755#1298#1260#1355" [39]=> string(23) "4351#772#1328#1292#1386" [40]=> string(23) "4451#790#1358#1324#1418" [41]=> string(23) "4551#807#1388#1355#1449" [42]=> string(23) "4651#824#1418#1387#1480" [43]=> string(23) "4751#841#1448#1419#1511" [44]=> string(23) "4851#859#1477#1450#1542" [45]=> string(23) "4951#876#1507#1482#1573" } }
질문
어떻게를 automaticly 페이지가 페이지를 새로 고침 할 필요없이 데이터를로드해야합니까?
내 passPHP()
기능으로이 자바 스크립트 코드를 삽입 한
내 자신의 시도 :이를 automaticly 페이지를 새로 고침하지만이 둔화했고 실제로 페이지를 보았다
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";
}
};
그 어레이 인덱스가 정의되지 않았다고 말했다.
- 참고 : 나는 이전 요청이 반환되면 값을로드 다시 AJAX를 사용한다 jQuery를
나는 정말로 당신이 의미하는 것을 얻지 못한다. 아약스는 jquery 만 맞습니까? – Bas
아니요 AJAX는 사용중인 방법입니다. 'A'' 동기식''J'vascript'A''와'X'ML. JavaScript에서는 이것을 위해'xmlHttpRequest'를 사용합니다. 이 내용은 시간이 지남에 따라 발전했으며이 유형의 요청을 통해 XML 외에도 텍스트, JSON 또는 기타 내용을로드 할 수 있습니다. AJAX는 데이터를 비동기로 보내고받을 수 있으므로 실행하는 동안 브라우저를 잠그지 않습니다. – Mouser
그래,하지만이 문제를 해결할 수있는 방법을 모르겠다. – Bas