Problem:
I have a variable which is encoded in JSON and I'm stuck trying to figure out how this variable can be sent to a PHP page using jQuery/AJAX.
This is what I have tried so far.
- The file is uploaded using http://blueimp.github.io/jQuery-File-Upload/
- It gets processed by jQuery according to the JS code below.
JS code:
<script>
$(function () {
'use strict';
// Server-side upload handler:
var url = 'process.php';
$('#fileupload').fileupload({
url: url,
autoUpload: true,
acceptFileTypes: /(\.|\/)(txt)$/i,
maxFileSize: 5000000, // 5 MB
done: function (e, data) {
setTimeout(function(){
$("#loading").html("Loading...");}, 1000);
var formUrl = 'exec.php',
// You'll have to re-encode to JSON, probably:
formPerspective = JSON.stringify(data.result.formPerspective),
// This is the newly added value ( Maybe this needs JSON aswell? )
formTxtfile = JSON.stringify(data.result.formTxtfile),
// Generate the form
$form = $('<form action="'+ formUrl +'" method="post"></form>')
.append('<input type="hidden" name="Perspective" value="' + formPerspective + '">')
.append('<input type="hidden" name="Datafile" value="' + formTxtfile + '">')
.append('<input type="hidden" name="form_submitted">');
// Submit the form
$form.submit();
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>
- As you can see, it calls the PHP handler which is according to the code below.
PHP code:
<?php
session_start();
$folder = 'upload';
if (!empty($_FILES))
{
// Set temporary name
$tmp = $_FILES['files']['tmp_name'];
// Set target path and file name
$target = $folder . '/' . $_FILES['files']['name'];
// Upload file to target folder
$status = move_uploaded_file($tmp, $target);
if ($status)
{
// Set session with txtfile name
$_SESSION['txtfile'] = $_FILES['files']['name'];
$text_file = file('upload/' . $_SESSION['txtfile']);
foreach ($text_file as $line_number => $line)
{
if (strpos($line, "\t") === 0)
{
// Remove commented column names and first \t
$dimensions = explode("\t", preg_replace("#\t/.+#", '', substr($line, 1)));
}
}
// Set dimensions
$_SESSION['dimensions'] = str_replace('\n', '', $dimensions);
$jsonReturn = array(
'formPerspective' => $_SESSION['dimensions'],
'formTxtfile' => $_SESSION['txtfile']
);
// Convert to JSON
$encode = json_encode($jsonReturn);
echo $encode;
}
}
?>
Desired solution:
I would like to take the information in $encode and send it using POST to the page "oe.lc". On OE.lc the variable is picked by using:
$_POST["Perspective"]
In addition, OE.lc checks if a form is submitted using:
$_POST["form_submitted"]
The JS should redirect to OE.lc upon submission (now it redirects to explorer.php).
Any ideas?