php form form ajax method to upload pictures

form form ajax method to upload pictures

First reference jquery.form.js

Front Code
<pre>
<form ID = "Form1">
<INPUT ID = "file_temporaryImage" type = "File" name = "file_temporaryImage" onChange = "TemporaryMedia ();" />
<INPUT type = "hidden" ID = " userpicpath ">
<IMG ID =" UserPic "the src =" "/>
</ form>
<Script>
function TemporaryMedia () {
// var Image0 = $ (" INPUT [name = 'file_temporaryImage'] "). Val () ;
// determine whether the upload control to select the picture
var Image = $ ( "# file_temporaryImage") Val ();.
iF ($ .trim (Image) == "") {
Alert ( "choose picture"!);
return;
}
// url submitted request processing
var actionUrl = "/home/kelatoupiao/sctpic/";
//开始ajax操作
$("#form1").ajaxSubmit({
type: "POST",
dataType: "json",
url: actionUrl,
data: {},
success: function (data) {
if (data.success == 1) {
$('#userpicpath').val(data.data);
$('#userpic').attr('src',data.data);
} else {
alert(data.msg);
}


}


});


}
</script>
</pre>


PHP后台代码
<pre>
//上传图片
public function sctpic()
{
$openid = $this->_check_login();
$spath = __DIR__ . '/../../../Public/kelatoupiao/uploads/' . $openid . '_' . time() . '.jpg';
$webpath='/kelatoupiao/uploads/' . $openid . '_' . time() . '.jpg';
if (move_uploaded_file($_FILES["file_temporaryImage"]["tmp_name"], $spath)) {
echo json_encode(array('success' => 1, 'msg'=>'上传成功','data'=>$webpath));
exit();
} else {
echo json_encode(array('success' => 0, 'msg'=>'网络繁忙','data'=>''));
exit();
}
}
</pre>

Guess you like

Origin www.cnblogs.com/newmiracle/p/11872655.html