Dear Sir
I tried my code and I think there's a small Problem I noticed my not thinking far. I am adding a Random number to the input text box and connecting the users selected Image name to it with your code.
1. This then makes the user selected image as: name01.jpg
2. that value printed in input text box with generated number as: 33434_name01.jpg
SO Sir when calling the php file to folder and the path is different.
Sir so I thought to add a Image upload when click the input text box...
Code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<style type='text/css'>
input[type=file] {
display:block;
height:0;
width:0;
}
</style>
<script type='text/javascript'>//<![CDATA[
$(function(){
$('input[type=text]').click(function() {
$('input[type=file]').trigger('click');
});
$('input[type=file]').change(function() {
var vals = $(this).val(),
val = vals.length ? vals.split('\\').pop() : '';
$('input[type=text]').val(val);
});
});//]]>
</script>
</head>
<body>
<input type="text" name="bigimage" id='bigimage'/>
<input type="file" name="file" id="file" class="upload-file" >
</body>
</html>
Adding your code works sir with the generated number "Not" but repeats the same image name.
Code:
<script type='text/javascript'>
$(window).load(function(){
$("#file").change(function(e){
var fname=$("#file").val().split('\\').pop().split('/').pop();
var rnum = $.trim($('#bigimage').val());
rnum = rnum.indexOf('_') > -1? rnum.substring(0, rnum.indexOf('_')) : rnum;
$('#bigimage').val(rnum + '_' + fname);
});
});
</script>
Sir, Could you please help me to get the random number to the Input text box file upload to be as: 34343_image.jpg (random number and Image name)
pleaseeee.
My Entire code is index.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link type="text/css" rel="stylesheet" href="css/main.css" media="all" />
<script type='text/javascript' src='js/jquery-1.8.3.js'></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<style type='text/css'>
input[type=file] {
display:block;
height:0;
width:0;
}
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function(){
var fileInput = $('.upload-file');
var maxSize = fileInput.data('max-size');
$('.upload-form').submit(function(e){
if(fileInput.get(0).files.length){
var fileSize = fileInput.get(0).files[0].size; // in bytes
if(fileSize>maxSize){
alert('file size is more then' + maxSize + ' bytes');
return false;
}else{
// alert('file size is correct- '+fileSize+' bytes');
}
}else{
alert('choose file, please');
return false;
}
});
});
});//]]>
var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"];
function ValidateSingleInput(oInput) {
if (oInput.type == "file") {
var sFileName = oInput.value;
if (sFileName.length > 0) {
var blnValid = false;
for (var j = 0; j < _validFileExtensions.length; j++) {
var sCurExtension = _validFileExtensions[j];
if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
blnValid = true;
break;
}
}
if (!blnValid) {
alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
oInput.value = "";
return false;
}
}
}
return true;
}
</script>
<!-- cehck box val to input txt box and send to db the input text box values taken from the checkbox-->
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$('.chkbx').click(function(){
var text = "";
$('.chkbx:checked').each(function(){
text += $(this).val()+',';
});
text = text.substring(0,text.length-1);
$('#textbx').val(text);
});
});//]]>
</script>
<!-- cehck box val to input txt box and send to db the input text box values taken from the checkbox-->
<!-- generate Random Number -->
<script type='text/javascript'>
window.onload=function(){
function randomNumber (m,n)
{
m = parseInt(m);
n = parseInt(n);
var YourRandomNumber = Math.floor( Math.random() * (n - m + 1) ) + m;
document.getElementById('bigimage').value = YourRandomNumber;
}
randomNumber (00000,99999);
}
</script>
<!-- generate Random Number -->
<!-- file upload whn click input text box-->
<!-- I changed my logic since adding a generated no to a Input text bog and _ image name is diferent to the user selected file name that goes to the folder (file.jpng) and image path
as in input text box (23232_img.jpg)-->
<script type='text/javascript'>//<![CDATA[
$(function(){
$('input[id=bigimage]').click(function() {
$('input[type=file]').trigger('click');
});
$('input[type=file]').change(function() {
var vals = $(this).val(),
val = vals.length ? vals.split('\\').pop() : '';
$('input[id=bigimage]').val(val);
});
});//]]>
</script>
<!-- file upload whn click input text box-->
</head>
<body>
<form class="upload-form" action="save.php" name="frmAdd" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Image upload</td>
<td>
<p class="msg"></p>
<p><label for="select_file">Select jpg / jpeg / gif / png file</label></p>
<!--30kb = 30000 bytes so less than 30KB the jascript validation is-->
<input type="file" name="file" id="file" class="upload-file" data-max-size="30000" onchange="ValidateSingleInput(this);" >
</td>
<tr>
<tr>
<td>Big image <!-- gets the value from Input file type to send the image path to MySQL DB --></td>
<td><input type='text' name='bigimage' id='bigimage' readonly /></td>
</tr>
<td></td>
<td>
<input type="submit" name="submit" value="submit">
</td>
</tr>
</table>
</form>
<div class="add_db_wrapper">
<a href="index.php">Grid Data</a>
</div>
<!-- chkbox validate -->
<script>try {$("form").on("click", ":checkbox", function(event){
$(":checkbox:not(:checked)", this.form).prop("disabled", function(){
return $(this.form).find(":checkbox:checked").length == 5;
});
});
} catch (error) { throw error; }
</script>
<!-- chkbox validate -->
<!-- limit description text-->
<script>try {function maxLength(el) {
if (!('maxLength' in el)) {
var max = el.attributes.maxLength.value;
el.onkeypress = function () {
if (this.value.length >= max) return false;
};
}
}
maxLength(document.getElementById("description"));
} catch (error) { throw error; }
</script>
<!-- limit description text-->
<!-- gets the value from Input file type to send the image path to MySQL DB -->
<!--New from Dynamic Drive Forums from:Sir jscheuer1 -->
<!-- gets the value from Input file type to send the image path to MySQL DB -->
<script type='text/javascript'>
$(window).load(function(){
$("#file").change(function(e){
var fname=$("#file").val().split('\\').pop().split('/').pop();
var rnum = $.trim($('#bigimage').val());
rnum = rnum.indexOf('_') > -1? rnum.substring(0, rnum.indexOf('_')) : rnum;
$('#bigimage').val(rnum + '_' + fname);
});
});
</script>
<!-- gets the value from Input file type to send the image path to MySQL DB -->
<!-- gets the value from Input file type to send the image path to MySQL DB --></td>
</body>
</html>
Sir, Could you please help me to get the random number to the Input text box file upload to be as: 34343_image.jpg (random number and Image name)
pleaseeee.
Thank you.
Bookmarks