Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add statement before return true

Status
Not open for further replies.

s4dadmin

Programmer
Apr 27, 2003
107
0
0
US
I want to disable the form submit buttons before submitting the form. I am not sure where to enter the code.

Code:
<SCRIPT LANGUAGE="JavaScript">
<!--
  function confirmSubmit()
  {
	if (document.form1.txtName.value.length < 1
	|| document.form1.txtURL.value.length < 1) 
	{
		alert("You must fill in both fields of the form.");
		return false;
	}
	
	var confirmForm = confirm(
		"Is this correct?\n" + document.form1.txtName.value +
		"\n" + document.form1.txtURL.value);
	if (confirmForm == true)
		return true;
	return false;
  }
  
  function confirmReset()
  {
  	var resetForm = confirm("Are you sure you want to clear the form?");
	if (resetForm == true)
		return true;
	return false;
  }
// -->
</SCRIPT>

<form action="process_add_link.asp" method="post" name="form1" id="form1" onsubmit="return confirmSubmit();" onreset="return confirmReset();">

After this statement
Code:
if (confirmForm == true)

I want to process this statement
Code:
document.form1.Clear.disable = true;
document.form1.Submit.disable = true;

then I want the script to return true and submit.
How do I do this?

Quincy
 
Code:
<SCRIPT LANGUAGE="JavaScript">
<!--

function confirmSubmit()
{
  if (document.form1.txtName.value.length < 1
  || document.form1.txtURL.value.length < 1) 
  {
    alert("You must fill in both fields of the form.");
    return false;
  }
    
  var confirmForm = confirm( "Is this correct?\n" + document.form1.txtName.value + "\n" + document.form1.txtURL.value);

  if (confirmForm)
  {
    document.form1.Clear.disabled = true;
    document.form1.Submit.disabled = true;
  }

  return confirmForm;
}
  
function confirmReset()
{
  var resetForm = confirm("Are you sure you want to clear the form?");
  
  return resetForm;
}

// -->
</SCRIPT>

<form action="process_add_link.asp" method="post" name="form1" id="form1" onsubmit="return confirmSubmit();" onreset="return confirmReset();">

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
You're welcome.

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
The buttons are being disabled; however, the page is immediately submitted so you do not see them as disabled.

Here is a different version so you can see for yourself.
Code:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--

function confirmSubmit()
{
  if (document.form1.txtName.value.length < 1
  || document.form1.txtURL.value.length < 1) 
  {
    alert("You must fill in both fields of the form.");
    return false;
  }
    
  var confirmForm = confirm( "Is this correct?\n" + document.form1.txtName.value + "\n" + document.form1.txtURL.value);

  if (confirmForm)
  {
    document.form1.Clear.disabled = true;
    document.form1.Submit.disabled = true;
  }

  // return confirmForm;
  return false; // so the form doesn't submit and you can see disabled
}
  
function confirmReset()
{
  var resetForm = confirm("Are you sure you want to clear the form?");
  
  return resetForm;
}

// -->
</SCRIPT>
</head>
<body>

<form action="?" method="post" name="form1" id="form1" onreset="return confirmReset();" onsubmit="return confirmSubmit();">
<input type="text" name="txtName"><br>
<input type="text" name="txtURL"><br>
<input type="submit" value="Submit" name="Submit"><input type="reset"  value="Reset" name="Clear">
</form>
</body>
</html>

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
s4dadmin,

if what you're really asking is to disable the button the moment it is clicked, a slight alteration to Chessbot's code could be:

Code:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--

function confirmSubmit()
{
  if (document.form1.txtName.value.length < 1
  || document.form1.txtURL.value.length < 1) 
  {
    alert("You must fill in both fields of the form.");
    [b]document.form1.Submit.disabled = false;
    document.form1.Clear.disabled = false;[/b]
    return false;
  }
    
  var confirmForm = confirm( "Is this correct?\n" + document.form1.txtName.value + "\n" + document.form1.txtURL.value);

  if ([b]![/b]confirmForm)
[b]  {
    document.form1.Clear.disabled = false;
    document.form1.Submit.disabled = false;
  }
  else
   document.form1.submit();[/b]
}
  
function confirmReset()
{
  var resetForm = confirm("Are you sure you want to clear the form?");
  return resetForm;
}

// -->
</SCRIPT>
</head>
<body>
<form action="?" method="post" name="form1" id="form1" onreset="return confirmReset();" onsubmit="[b]return false;[/b]">
<input type="text" name="txtName"><br>
<input type="text" name="txtURL"><br>
<input type="submit" value="Submit" name="Submit" [b]onclick="this.disabled=true;document.form1.Clear.disabled=true;confirmSubmit();"[/b]>
<input type="reset"  value="Reset" name="Clear">
</form>
</body>
</html>

It occurs to me you might be after something like this if you're worried about the user hitting the submit button more than once.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top