尝试在worpress网站中使用jquery form plugin。
HTML:
<form id="imageform" method="post" action="<?php bloginfo('template_directory')?>/ajaximage.php " enctype="multipart/form-data" accept-charset="utf-8" >
Upload image:
<input type="file" name="photoimg" id="photoimg" value="" />
</form>
<button value=" Upload " class="btn_upload"> Upload </button>
<div id='preview'></div><!-- end of id preview-->
JS:
$(document).ready(function(){
$(".btn_upload").click(function(){
$("#preview").html('');
$(".result_upload").html('<img src="'+loc+'/images/ajax-loader.gif" alt="wait.."/>');
$("#imageform").ajaxForm({
target: '#preview'
}).submit();
});
});
ajaximage.php有
$photoimg = trim($_POST['photoimg']);
echo 'got it';
如果我没有选择任何文件,我会按预期得到结果。但是如果我选择一个图像文件,那么错误信息会显示 - “Undefined index:photoimg in line ....”
如何让它正常工作?
答案 0 :(得分:1)
您是否尝试过$ _FILES而不是$ _POST? http://php.net/manual/en/reserved.variables.files.php
(提交文件时,它在$ _FILES内引用而不是$ _POST。)