Skip to content

Instantly share code, notes, and snippets.

@uedatakeshi
Created April 30, 2009 08:27
Show Gist options
  • Save uedatakeshi/104350 to your computer and use it in GitHub Desktop.
Save uedatakeshi/104350 to your computer and use it in GitHub Desktop.
file upload
<html>
<head>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript" src="./form.js"></script>
<script type="text/javascript">
var reg = /(\d+)$/i;
$(document).ready(function() {
$(':button').click(function(){
var id = $(this)[0].id.match(reg)[0];
$('#myForm').ajaxSubmit({url: 'result.php?id='+id, success: upImage});
return false;
});
$("input[name^='img']").each(function(){
if ($(this).attr('value')){
id = $(this).attr('name').match(reg)[0];
$('#output'+id).html('<a href="">'+$(this).attr('value')+'</a><label><input type="checkbox" name="del' +id+ '" value="1" />delete</label>');
}
});
delCheck();
});
function upImage(responseText, statusText) {
if (responseText) {
var res = responseText.split("_");
$('#img' + res[0]).val(res[1]);
$('#output' + res[0]).html('<a href="">'+res[1]+'</a><label><input type="checkbox" name="del' + res[0] + '" value="1" />delete</label>');
$('<div class="error-message">'+res[2]+'</div>').insertAfter('input[id="button'+res[0]+'"]');
delCheck();
}
}
function delImage(responseText, statusText) {
var res = responseText.split("_");
$('#img' + res[0]).val('');
$('#output' + res[0]).html('');
}
function delCheck() {
$("input:checkbox[name^='del']").click(function(){
var del_id = $("input:checkbox:checked[name^='del']")[0].name.match(reg)[0];
$('#myForm').ajaxSubmit({url: 'result.php?id='+ del_id, success: delImage});
return false;
});
}
</script>
</head>
<body>
<form id="myForm" action="comment.php" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" />
<input type="checkbox" name="ji" checked="checked">
Comment: <textarea name="comment"></textarea>
<input type="file" name="solid1">
<input type="button" id="button1" value="upload" />
<input type="hidden" name="img1" id="img1" value="houhou.jpg" />
<input type="file" name="solid2">
<input type="button" id="button2" value="upload" />
<input type="hidden" name="img2" id="img2" value="" />
<input type="submit" value="Submit Comment" />
</form>
<div id="output1"></div>
<div id="output2"></div>
<div id="message1"></div>
<div id="message2"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment