[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1419
  • 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
 
HonorGodCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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