1

I have a piece of javascript that uses Ajax and PHP to upload an image.

I am using the javascript in the head section of a few html pages and was wondering if there was a way of passing a value from these pages to the Upload.php file, without having to create individual Upload.php files for each web page.

Section of the javascript:

<script type="text/javascript" >
        $(function(){
            var btnUpload=$('#me');
            var mestatus=$('#mestatus');
            var files=$('#files');
            new AjaxUpload(btnUpload, {
                action: '../UploadDoc/Upload.php',
                name: 'uploadfile',

Section of the Upload.php file i would like to pass value to:

<?php
/* Set the location to upload the file to */
$uploaddir = '../documents/userdocs/'; 

/* Set the name and file extension of the file being saved */
$file = $uploaddir ."HtmlPage1_".basename($_FILES['uploadfile']['name']); 
$file_name= "HtmlPage1_".$_FILES['uploadfile']['name']; 

Where 'HtmlPage1_' will be the information i need to pass in, as it is the only part that changes.

Any help much appreciated.

Many thanks

4

1 回答 1

1

在你的 Javascript 中做

$(function(){
        var btnUpload=$('#me');
        var mestatus=$('#mestatus');
        var files=$('#files');
        new AjaxUpload(btnUpload, {
            action: '../UploadDoc/Upload.php',
            name: 'uploadfile',
            params: {
                  pageKey: 'HtmlPage1_'
            }
      ......ETC-CODE......................

或者你的javascript代码

$(function(){
        var btnUpload=$('#me');
        var mestatus=$('#mestatus');
        var files=$('#files');
        new AjaxUpload(btnUpload, {
            action: '../UploadDoc/Upload.php?pageKey=HtmlPage1_',
            name: 'uploadfile',
      ......ETC-CODE......................

在你的 PHP 中做:

<?php
/* Set the location to upload the file to */
$uploaddir = '../documents/userdocs/'; 

/* Set the name and file extension of the file being saved */
$file = $uploaddir .$_REQUEST['pageKey'].basename($_FILES['uploadfile']['name']); 
$file_name= $_REQUEST['pageKey'].$_FILES['uploadfile']['name']; 
......ETC-CODE......................

然后将每个页面中的 javascript 代码修改为“pageKey”参数。

于 2012-05-30T19:30:58.970 回答