Joomla 3.0 custom module: file upload on ajax submitted form(Joomla 3.0 自定义模块:在 ajax 提交表单上上传文件)
问题描述
目前我正在开发一个自定义模块,其中包含一个表单并通过 ajax 提交.
Currently I'm developing a custom module, containing a form and submitted 'through' ajax.
领域
<input type="file" id="file" name="file" accept="image/*" disabled="disabled" />
ajax 提交(使用 json)
ajax submit (with json)
$.ajax({
type: "POST",
url: "modules/mod_custom_form/submit_form.php",
data: dataString,
dataType: "JSON",
timeout: 6000,
success: function(response) {
// on success
if (response.success === 1) {
$('#customForm').html("<div id='message'></div>");
$('#message').html("<h2>Form sent!</h2>")
.append("<p>More text.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src="bW9kdWxlcy9tb2RfY3VzdG9tX2Zvcm0vaW1hZ2VzL2NoZWNrLWljb24ucG5n" />");
});
}
// on failure
else {
$('#customForm').html("<div id='message'></div>");
$('#message').html("<h2>Failure.</h2>");
}
}
});
submit_form.php 文件
submit_form.php file
// no direct access
define('_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
// sender email
$email = 'maarten@mail.com';
$subject = 'Aanvraag';
// create the header array
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: beheer <maarten@mail.com>";
$headers[] = "Bcc: beheer <maarten@mail.com>";
$headers[] = "Reply-To: Recipient Name <receiver@domain3.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/" . phpversion();
// get all posted data and put them in variables
$bedrijfsnaam = $_POST['bedrijfsnaam'];
$_FILES = $_POST['uploaded_file'];
// create the full message
$message = 'email';
// send mail
if (mail($to, $subject, $message, implode("
", $headers))) {
//save file function
getInput($_FILES);
// return message
echo json_encode(array('success' => 1));
// insert the email into the database
$database =& JFactory::getDBO();
$query = "INSERT INTO #__email_forms (mid, email, message) VALUES ('2', 'myemail@mail.com', '$message')";
$database->setQuery($query);
$database->query();
}
else {
echo json_encode(array('success' => 0));
}
我将如何实现上传文件并使用 ajax 保存表单的可能性?我已经找了几个小时了,但到目前为止还没有找到任何有用的东西.
How would I implement the possibility to upload a file and save the form with ajax? I've been looking for hours, but haven't found anything useful so far.
预先感谢您的建议.
推荐答案
文件的 Ajax 上传是一件棘手的事情,特别是如果您涉及会话数据以确保用户上传实际上是被允许这样做的.当它是公开上传时,它会变得更加危险,因为您需要进行更多的服务器端验证.
Ajax uploading of files is a tricky thing, especially if you have session data involved to ensure that the user uploading is actually permitted to do so. It get's even more dangerous when it's a public upload, as you need to do a lot more server side validations.
我通常用于 ajax 上传的是 valums 的 Ajax 文件上传器:https://github.com/valuems/file-uploader
What I typically use for ajax upload is the Ajax File Uploader by valums here: https://github.com/valums/file-uploader
运行起来非常简单,而且它有一个 jQuery 插件,使它更简单(因为您已经在使用 jQuery).
It's pretty straightforward to get running, and it has a jQuery plugin for it as well to make it even simpler (since you're already using jQuery).
这篇关于Joomla 3.0 自定义模块:在 ajax 提交表单上上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Joomla 3.0 自定义模块:在 ajax 提交表单上上传文件
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 仓库 2022-01-01