Link to home
Start Free TrialLog in
Avatar of earwig75
earwig75

asked on

Display Javascript Confirmation box on form submit with form contents

I'd like to pop open a javascript alert box and show the details of a form before submitting.

Below is what I have so far for the JavaScript. Could someone assist? Thank you.

<form name="myForm" onsubmit="confirm()">
<input type="text" id="theNumber" name="theNumber" />
<input type="text" id="theName" name="theName" />

<script language="javascript">
function Confirm(){
      var theNumber = document.getElementById("theNumber").value;
      var theName = document.getElementById("theName").value;
	var r=confirm("Are you sure you want to submit these details?");
		if (r==true)
		 {
//show something like the below here
The Number: theNumber here
The Name: theName here

		 }
		else
		 {
		  alert("You pressed Cancel!");
		 }
</script>

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

try:
<form name="myForm" action="#" onsubmit="return Confirm()">
<input type="text" id="theNumber" name="theNumber" />
<input type="text" id="theName" name="theName" /><input type="submit"/>
</form>
<script language="javascript">
function Confirm(){
      var theNumber = document.getElementById("theNumber").value;
      var theName = document.getElementById("theName").value;

		var r=confirm("Are you sure you want to submit these details?");
		
		if (r==true)
		 {
			var str="You submitted:";
			  str+="\n    Name: " + theName;
			  str+="\n  Number: " + theNumber;
			  alert(str);
			return true;
		 }
		else
		 {
		  alert("You pressed Cancel!");
		 }
return false;
}
</script>

Open in new window


NOTE: Javascript is case sensitive, so you need onsubmit="Confirm()" -- with capital "C"
Avatar of earwig75
earwig75

ASKER

I apologize, I need the form details to come before the if r==true... I'd like them to see the form details before clicking Ok.
Also, when I pressed cancel it submitted the form instead of returning false.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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