【问题标题】:Joomla 3.0 custom module: file upload on ajax submitted formJoomla 3.0 自定义模块:在 ajax 提交表单上上传文件
【发布时间】:2013-03-21 13:40:22
【问题描述】:

目前我正在开发一个自定义模块,其中包含一个表单并“通过”ajax 提交。

领域

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

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 文件

// 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));
}

我将如何实现上传文件并使用 ajax 保存表单的可能性?我一直在寻找几个小时,但到目前为止还没有找到任何有用的东西。

提前感谢您的建议。

【问题讨论】:

  • 你有什么问题?
  • 查看更新的问题:)
  • 这两个文件是否在同一个文件夹中?贴出submitform.php的代码
  • 我不太清楚您的意思,我想将文件保存在 media 或 images 文件夹中。表单文件和 submit_form.php 文件位于同一个模块文件夹中。
  • 贴出submitform.php的代码。以及当您尝试在 submit-form.php 中打印时获得的数据。

标签: php file joomla upload


【解决方案1】:

Ajax 上传文件是一件棘手的事情,尤其是当您涉及到会话数据以确保实际允许用户上传时。当它是公开上传时,它会变得更加危险,因为您需要做更多的服务器端验证。

我通常用于 ajax 上传的是 Ajax File Uploader by valums:https://github.com/valums/file-uploader

运行起来非常简单,它还有一个 jQuery 插件,让它变得更简单(因为你已经在使用 jQuery)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多