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.
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>
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?
Try to simply echo the $encode and then exit the script by using die();
Edit: You said you would like to add extra values to the form, first you need to change the php file that determines the return to Javascript in JSON:
$jsonReturn = array(
'formPerspective' => $dimensions,
'formTxtfile' => $_SESSION['txtfile'],
);
// Convert to JSON
$encode = str_replace('\n', '', json_encode($jsonReturn));
// The above will print out the following when echoed:
// {'formPerspective':["legs","skincover","weight","intelligence","speed"], 'formTxtfile': 'Not sure what this holds as data'}
die($encode);
The data should then be in data.result in the done callback you configured.
You probably want to forward the browser to oe.lc, how about you generate a form and submit that?
Edit: We should add our newly accuired value here:
$('#fileupload').fileupload({
// ...
done: function (e, data) {
// Determine the url:
var formUrl = 'http://oe.lc',
// 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'>")
.appendTo('body');
// Submit the form
$form.submit();
},
// ...
});
Please let me know if that works for you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With