简体   繁体   English

Joomla 3.0自定义模块:在ajax提交的表单上上传文件

[英]Joomla 3.0 custom module: file upload on ajax submitted form

Currently I'm developing a custom module, containing a form and submitted 'through' ajax. 目前我正在开发一个自定义模块,其中包含一个表单并提交了“通过”ajax。

the field

<input type="file" id="file" name="file" accept="image/*" disabled="disabled" />

ajax submit (with json) ajax提交(与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='modules/mod_custom_form/images/check-icon.png' />");
              }); 
            } 
            // on failure
            else {
                $('#customForm').html("<div id='message'></div>"); 
              $('#message').html("<h2>Failure.</h2>");
            }
        }
      }); 

submit_form.php file submit_form.php文件

// 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("\r\n", $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));
}

How would I implement the possibility to upload a file and save the form with ajax? 我如何实现上传文件并使用ajax保存表单的可能性? I've been looking for hours, but haven't found anything useful so far. 我一直在寻找好几个小时,但到目前为止还没有找到任何有用的东西。

Thanks in advance for your advice. 提前感谢您的建议。

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. Ajax上传文件是一件棘手的事情,特别是如果您涉及会话数据以确保实际允许用户上传。 It get's even more dangerous when it's a public upload, as you need to do a lot more server side validations. 当它是公共上传时,它会变得更加危险,因为您需要进行更多的服务器端验证。

What I typically use for ajax upload is the Ajax File Uploader by valums here: https://github.com/valums/file-uploader 我通常用于ajax上传的是valums的Ajax File Uploader: https//github.com/valums/file-uploader

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). 它的运行非常简单,它有一个jQuery插件,以使它更简单(因为你已经在使用jQuery)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM