如何在JavaScript函数中传递一个php代码?
我想将上传的文件名添加到('.list')中。文件的名称必须是它在服务器上传时被调用的名称。例如,我可以有2个文件,但有一个被称为mountains.png,另一个是mountains2.png。如何在JavaScript函数中传递一个php代码?
但问题是,我怎么能传递$ _FILES [“fileImage”] [“name”]作为我的js函数的参数,然后追加它,因为JavaScript函数和PHP脚本是在单独的页面上PHP脚本确实做了一个回调函数)?
UPDATE
下面是JavaScript代码:
下面是形式代码(QandATable.php)
<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startImageUpload(this);' class='imageuploadform' >
<p>Image File: <input name='fileImage' type='file' class='fileImage' />
<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' />
</p>
<ul class='list'></ul>
</form>
下面是JavaScript函数(QandATable.php)
function stopImageUpload(success){
var nameimagefile = <?php echo $nameimagefile?>;
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').append(nameimagefile + '<br/>');
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
return true;
}
下面是t他的PHP脚本(imageupload.php):
$result = 0;
$nameimagefile = '';
if(file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while(file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$nameimagefile = $_FILES["fileImage"]["name"];
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$nameimagefile = $_FILES["fileImage"]["name"];
}
?>
<script language="javascript" type="text/javascript">window.top.window.stopImageUpload(<?php echo $result;?>);</script>
你可以简单地把价值$ _FILE文件名到一个PHP变量比使用
var yourjasvariable=<?php echo $yourvariable?>;
呼应,并使用这个js在append方法变量。 :-)
只是一个简单的问题,$ yourvariable = $ _FILES [“fileImage”] [“name”];进入php脚本页面(imageupload.php),还是与javascript函数在同一页面上? – user1324106 2012-04-18 11:38:31
好吧。如果您使用2个不同的页面,则应该查看Ajax/JSON以在Javascript和PHP之间交换数据。 – SativaNL 2012-04-18 11:43:20
将文件名称作为另一个参数传递,如果存在多个文件,则可以使用数组。 $ yourvariable = $ _FILES [“fileImage”] [“name”];这将只存在于你的imageupload.php中。谢谢 – 2012-04-18 11:46:02
你可以选择AJAX做你想做的。 用JSON编写数据。 JSON可以从PHP和JavaScript 阅读 - 阅读JSON在PHP 获取数据 - 读取AJAX结果(JSON)从PHP
获取数据我会做这样的事情(未经测试的例子)
AJAX JS部分
<form method='post' enctype='multipart/form-data' onsubmit='startAjaxImageUpload(this);' >
...
</form>
/*
* ajax functions
*/
function startAjaxImageUpload(event){
/* Collect your formdatas as json with jquery this datas will be sent to php*/
var formDatas = {
'value1' : $('input[test1=eid]').val(),
'value2' : $('input[id=test2_id]').val(),
......
'value3' : $('input[id=test3_id]').val()
};
$.ajax({
cache: false,
url: "imageupload",
data: formDatas,
success: function(data) {
// data is the json Result from php => imageupload.php do what u want with them in js
// use the next line if u wanna see which json datas comes back from php if the ajax call wass successfull
// console.log("data is %o, data);
// ....
}
error:function(data){
// error function
// data is the json Result from php => imageupload.php do what u want with them in js
// use the next line if u wanna see which json datas comes back from php if the ajax call wass successfull
// console.log("data is %o, data);
alert(damn, something went wrong);
}
})
}
PHP的一部分,imageupload.php
$result = 0;
$nameimagefile = '';
.....
// if done ure work on server side and no error was found, pass the result back to starAjaxImageUpload success function
return $nameimagefile = $_FILES["fileImage"]["name"];
}else
// abbort ajax, ajax error function will used
return false
}
你必须寻找到AJAX为 – hjpotter92 2012-04-18 11:30:11