Jump to content

[HELP] how to upload (or send) files in in jquery..?


gratsami

Recommended Posts

hi ,

 

i faced a problem in upload files in jQUERY & PHP.

how i can send the file to upload.php and get response..

 

this my code...

form.php

<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript" src="./jquery.validate.js"></script>
<script type="text/javascript">
$().ready(function() {
[color=red]$("#picForm").attr("enctype", "multipart/form-data").attr("encoding", "multipart/form-data");[/color]
$.validator.setDefaults({
	submitHandler: function() {
		var titleVal = $("#title").val();
		var sectionVal = $("#section").val();
		var contentVal = $("#content").val();
		var picVal = $("#pic").val();
		var myidVal = $("#myid").val();
		$("#msgbox2").removeClass().addClass('messagebox')
			.append('<img src="../images/loading.gif" alt="Loading" id="loading" />').fadeIn(3000);
		$.post("./upload.php", { title: titleVal, section: sectionVal,
		content: contentVal, pic: picVal, myid: myidVal },function(data){
			$("#msgbox2").fadeTo(300,0.1,function(){
				$(this).html(data).addClass('messageboxok').fadeTo(3000,1);
			});
			$("#title").val(''); $("#content").val(''); $("#pic").val('');
		});
	}
});
$("#picForm").validate({
	rules: {
		title:"required", pic: { required: true, [color=green]accept: "gif|jpg|jpeg"[/color] }
	},
	messages: {
		title:{ required: " * " }, pic:{ required: " * " , [color=green]accept: "available Extentions:<br>(gif,jpeg,jpg)"[/color] }
	}
});
});
</script>
<div id="messageall">
<span id="msgbox2" style="display:none"></span>
</div>

<form method="post" name="picForm" id="picForm" enctype="multipart/form-data">
<input type="hidden" name="myid" id="myid" size="5" value="19830214">
<label>Title</label><input type="text" name="title" id="title" size="30" class="required">
<br />
<label>Section</label>
<select name="section" id="section" size="1">
<option value="1">section#1</option>
<option value="2">section#2</option>
<option value="3">section#3</option>
<option value="4">section#4</option>
</select>
<br />
<label>Description</td><textarea name="content" id="content" cols="50" rows="7"></textarea>
<br />
<label>File:</label><input type="file" name="pic" id="pic" size="30" class="required [color=green]accept[/color]">
<br />
<input type="submit" name="submit" id="submit" value="Send...">
</form>

 

upload.php

<?php
$File = @$_FILES['pic']['tmp_name'];
if(is_uploaded_file($File)){
$Filename=$_FILES['pic']['name'];
$ext = substr(strrchr($Filename, '.'),1);
$rnd = time().rand(333,999).".".$ext;
if(move_uploaded_file($_FILES['pic']['tmp_name'],'./uploads/'.$rnd)){
	echo "success";
	echo $_FILES['pic']['name'];
	echo "<br />";
	echo $_FILES['pic']['type'];
	echo "<br />";
	echo $_FILES['pic']['size'];
}else{
	echo "error";
}
}else{
echo "no file to upload";
}
?>

 

question : is red code in form.php right?

another question : can i validate file size in jquery like (accept)?

 

please help me ... iam waiting!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.