Link to home
Start Free TrialLog in
Avatar of conrad2010
conrad2010

asked on

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

Avatar of HonorGod
HonorGod
Flag of United States of America image

Define a function (elsewhere), and have the onclick call the function.
Avatar of conrad2010
conrad2010

ASKER

code sample for function would be appreciated...
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

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

ASKER CERTIFIED SOLUTION
Avatar of sunithnair
sunithnair

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
great solution!