• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1491
  • Last Modified:

Javascript PROMPT on form submit

I have a form with a "Save" image as the submit button...
Clicking on the image submits the form...
I would like a JavaScript prompt to appear before form submission asking for the file name...
The value is entered in the javascript window...
The form is submitted with the PROMPT value...

How can I do this in one line?
<INPUT type="image" src="save.gif" onClick="return var my_string = prompt("Please enter file name","enter file name only"); document.write(my_string);">

Open in new window

0
conrad2010
Asked:
conrad2010
  • 3
  • 2
1 Solution
 
HonorGodSoftware EngineerCommented:
Define a function (elsewhere), and have the onclick call the function.
0
 
conrad2010Author Commented:
code sample for function would be appreciated...
0
 
sunithnairCommented:
Try this one
<html>
<head>
<script language='javascript'>
function showPrompt()
{
   fileName = prompt("Please enter file name","enter file name only");
   alert(fileName);
   if(fileName != null)
   {
     document.getElementById("filename").value=fileName;
     return true;   
   }
   else
     return false;
}
</script>
</head>
<body>
<form>
<INPUT type="image" src="save.gif" onClick="return showPrompt();">
<input type="hidden" name="filename" id="filename">
</form>
</body>
</html>

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
sunithnairCommented:
The abouve code will not post the form if the file name is not entered (i.e. if the user clicks the cancel button). The code below will not postback if a blank filename is entered and presses cancel
<html>
<head>
<script language='javascript'>
function showPrompt()
{
   fileName = prompt("Please enter file name","enter file name only");
   if(fileName != null && fileName!="")
   {
     document.getElementById("filename").value=fileName;
     return true;   
   }
   else
     return false;
}
</script>
</head>
<body>
<form>
<INPUT type="image" src="save.gif" onClick="return showPrompt();">
<input type="hidden" name="filename" id="filename">
</form>
</body>
</html>

Open in new window

0
 
sunithnairCommented:
You can then get the value of the fine name entered in the server side post or get variable "filename"
0
 
conrad2010Author Commented:
great solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now