0

I have a array student. I need to pass this array in another php page via POST, not from GET, because it can contains thousands of characters.

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.

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));
4
  • 1
    surely the above will effectively make 2 requests to sheet.php - a POST request and then, when that succeeds, a GET request? Commented Apr 14, 2019 at 21:08
  • if you are using jquery just use this without the above code $.post('sheet.php',{mnu:student},function() {window.open('sheet.php','_blank')}); Commented Apr 14, 2019 at 21:33
  • @Talal, I tried using $.post, it redirects but same problem undefined index $_POST['mnu']. Is there any other way to access ?? I am doing simply if(isset($_POST['mnu'])){echo $_POST['mnu'];} Commented Apr 14, 2019 at 21:42
  • dude the ajax request would have data posted after the success of the request it would open sheet.php and this time there is no data posted Commented Apr 14, 2019 at 21:46

2 Answers 2

2

Like @RamRaider commented.. you're making two requests to sheet.php. The first being a "silent" POST request and the second being a GET request after the first POST request has successfully completed.

Request count

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..

$('<form>', {
    action: 'sheet.php',
    method: 'POST',
    target: '_blank',
    html: $('<input>', {
        name: 'mnu',
        value: JSON.stringify(student)
    }).prop('outerHTML')
}).appendTo($('body')).submit().remove();

Sign up to request clarification or add additional context in comments.

Comments

0

Change http.send('mnu='+JSON.stringify(student)); to http.send(JSON.stringify(student));

And then in your sheet.php use json_decode($_POST) to fetch your POST data

2 Comments

what to decode in php file ??, json_decode() expects 1 parameter, and it doesn't student array
http.send(JSON.stringify(student)); means you turn your JSON object into a string. Once posted to the page sheet.php you need to turn it back to an object, so you can use something like $student = json_decode($_POST). Then you should be able to access your student directly as an object (e.g $student->name)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.