How can I go about passing data from a HTML form to a JavaScript file that handles AJAX calls?
submission_form.php
<?php echo elgg_view("input/file", array("name" => "icon", "class" => "icon")); ?>
<p>
<?= elgg_view('input/text', ["name" => 'title', "class" => "title"]); ?>
</p>
submit.js
function myajax_function(){
var Ajax = require('elgg/Ajax');
var ajax = new Ajax();
ajax.action('service_comments/add_service_comment', {
data: {
picture: 1,
description: 2
},
}).done(function (output, statusText, jqXHR) {
if (jqXHR.AjaxData.status == -1) {
return;
}
alert(output.sum);
alert(output.product);
});
}
action file : submission_form.php
// Not working
$title = get_input('title');
// I'm not sure how to go about extracting the file from input/file and text from input/text
Thank you all in advance
info@elgg.org
Security issues should be reported to security@elgg.org!
©2014 the Elgg Foundation
Elgg is a registered trademark of Thematic Networks.
Cover image by RaĆ¼l Utrera is used under Creative Commons license.
Icons by Flaticon and FontAwesome.
- ihayredinov@ihayredinov
ihayredinov - 0 likes
- ihayredinov@ihayredinov
ihayredinov - 0 likes
You must log in to post replies.Elgg Ajax does not send multipart form data.
You can either use Javascript `File` API to read raw bytes and send them with your non-multipart request
or
Use jQuery Form plugin bundled in Elgg (`jquery.form` AMD module) - use $form.ajaxSubmit() with an iframe set to true. You may need to add X-Requested-With header to fool Elgg into believing it's an XHR request, so you get JSON back. Here is an overly complicated example (can't find anything simpler): https://github.com/hypeJunction/hypeWall/blob/master/views/default/js/framework/wall/lib.js#L203
To send regular form data with elgg.action request, use $form.serialize(). There should be an example in the ajax docs.