0

hi i am using php and mysql.

i have a form to add record for user. user should prompt when he will not select any file to upload that you want to save data without image.

Can we do that using javascript..

if any one know it would be appreciated..

Thanks in advance

Gaurav
  • 28,447
  • 8
  • 50
  • 80
Sanjay Khatri
  • 4,153
  • 7
  • 38
  • 42
  • "user should prompt when he will not select any file to upload that you want to save data without image." i.e. you have some list of images to be select by user if (s)he not uploading any image,, I'm i right... – Naresh Mar 21 '11 at 06:01

2 Answers2

2

You can verify the file input using javascript.

Here is an example:

function checkFile(yourForm){

    var fileVal = yourForm.elements['fileField'].value;

    //RegEx for valid file name and extensions.
    var pathExpression = "[?:[a-zA-Z0-9-_\.]+(?:.png|.jpeg|.gif)";


    if(fileVal != ""){
        if(!fileVal.toString().match(pathExpression) && confirm("The file is not a valid image. Do you want to continue?")){
            yourForm.submit();
        } else {
            return;
        }
    } else {
        if(confirm("Do you want to continue without adding image?")) {
            yourForm.submit();
        } else {
            return;
        }
    }
}

In your html

<form name="yourForm" method="post" enctype="multipart/form-data"">
    <input type="file" name="fileField" />
    <input type="button" value="submit" onClick="checkFile(this.form)" />
</form>

Note pathExperssion can be customized. Another thing is checking the extension is not too reliable as anyone can inject a file name as extension. Click here for other info.

Community
  • 1
  • 1
ace
  • 6,775
  • 7
  • 38
  • 47
1

just check to see if the field is empty and then ask them if that's really what they want...

if( document.forms["uploadForm"].elements["imageField"].value == "" 
     && confirm("You have not added an image, are you sure you want to continue?")
 )
        upload();
    else
        doNothing();
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236