[英]How to access array in another page after sending through ajax
I have a array student
. 我有一群
student
。 I need to pass this array in another php page via POST
, not from GET
, because it can contains thousands of characters. 我需要通过
POST
而不是从GET
将此数组传递到另一个php页面中,因为它可以包含数千个字符。
I am trying to open new page sheet.php
and echo the array student
, I do simply checking echo $_POST['mnu']
, but it is showing undefined index error. 我正在尝试打开新页面
sheet.php
并回显student
数组,我只是在检查echo $_POST['mnu']
,但是它显示未定义的索引错误。
var http = null;
if(window.XMLHttpRequest){
http = new XMLHttpRequest();
}
else{
http = new ActiveXObject('Microsoft.XMLHTTP');
}
http.open('POST','sheet.php',true);
http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
http.onreadystatechange = function(){
if(http.readyState==4 && http.status==200){
window.open('sheet.php','_blank')
}
}
http.send('mnu='+JSON.stringify(student));
Like @RamRaider commented.. you're making two requests to sheet.php
. 就像@RamRaider评论..您正在对
sheet.php
发出两个请求。 The first being a "silent" POST request and the second being a GET request after the first POST request has successfully completed. 第一个是“静默” POST请求,第二个是在成功完成第一个POST请求之后的GET请求。
The second request won't share the payload of the first. 第二个请求将不会共享第一个请求的有效负载。
If I under stand correctly the below code should do what you are wanting... 如果我正确理解以下代码,那么您应该做的是...
// Create a form element
// <form action="sheet.php" method="post"></form>
var tempForm = document.createElement('form');
tempForm.setAttribute('action', 'sheet.php');
tempForm.setAttribute('method', 'POST');
tempForm.setAttribute('target', '_blank'); // Open in new tab
// Create an input field
// <input name="mnu" value="...">
var tempInput = document.createElement('input');
tempInput.setAttribute('name', 'mnu');
tempInput.setAttribute('value', JSON.stringify(student)); // Set field value
// Add the input to the form
tempForm.appendChild(tempInput);
// Add the form to the body in order to post
document.body.appendChild(tempForm);
// Submit the form
tempForm.submit();
// Remove the form
document.body.removeChild(tempForm);
And if you're using jQuery you can simplify the above code.. 而且,如果您使用的是jQuery,则可以简化上面的代码。
$('<form>', {
action: 'sheet.php',
method: 'POST',
target: '_blank',
html: $('<input>', {
name: 'mnu',
value: JSON.stringify(student)
}).prop('outerHTML')
}).appendTo($('body')).submit().remove();
Change http.send('mnu='+JSON.stringify(student));
更改
http.send('mnu='+JSON.stringify(student));
to http.send(JSON.stringify(student));
到
http.send(JSON.stringify(student));
And then in your sheet.php
use json_decode($_POST)
to fetch your POST data 然后在
sheet.php
使用json_decode($_POST)
来获取POST数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.