2013-03-21 71 views
1

目前我正在开发一个自定义模块,其中包含一个表单,并提交'通过'阿贾克斯。Joomla 3.0自定义模块:在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 = '[email protected]'; 
$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 <[email protected]>"; 
$headers[] = "Bcc: beheer <[email protected]>"; 
$headers[] = "Reply-To: Recipient Name <[email protected]>"; 
$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', '[email protected]', '$message')"; 
    $database->setQuery($query); 
    $database->query(); 
} 
else { 
    echo json_encode(array('success' => 0)); 
} 

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

在此先感谢您的建议。

+0

你的问题是什么? – Toretto 2013-03-21 13:52:41

+0

看到更新的问题:) – 2013-03-21 13:55:58

+0

你有两个文件在同一个文件夹?发布submitform.php代码 – Toretto 2013-03-21 14:00:20

回答

3

上传文件的Ajax是一件棘手的事情,特别是如果您有会话数据参与以确保用户上载实际上允许这样做。当它是一个公共上传时,它会变得更加危险,因为你需要做更多的服务器端验证。

我通常使用AJAX上传阿贾克斯由valums文件上传这里:https://github.com/valums/file-uploader

这是很简单的得到运行,而且它有一个jQuery插件,它并使它更简单(因为你已经使用jQuery)。