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?
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);">
Define a function (elsewhere), and have the onclick call the function.
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>
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great solution!