PDA

View Full Version : Cant make the submit button disable



CraigH
08-25-2007, 12:43 AM
Hello all,

I've seen the posts on this, but the submit button is not disabling on my page. The code is supposed to check the form for errors and disable the submit button. It catches the errors, but won't disable the submit. Here's the JS in the head:

function disableForm(theform) {
if (document.all || document.getElementById) {
for (i = 0; i < theform.length; i++) {
var tempobj = theform.elements[i];
if (tempobj.type.toLowerCase() == "submit")
tempobj.disabled = true;
}
// setTimeout('alert("Sending information...")', 2000);
return true;
}
}


function validRequired(formField,fieldLabel)
{
var result = true;

if (formField.value == "")
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}

return result;
}

function validateForm(theForm){
if (!validRequired(theForm.cardnumber,"Credit Card Number")){
return false;
}

if (!validRequired(theForm.cardexpmonth,"Expiration Month")){
return false;}

if (!validRequired(theForm.cardexpyear,"Expiration Year")){
return false;}

disableForm(theForm);
return true;
}


Here's the form line:

<form name="addjob3" action="<? PHP_SELF; ?>" method="POST" onsubmit="return validateForm(this)">

Here's the submit button:

<input type="submit" name="submitp" value="Submit">

Thanks in advance!

CraigH
08-26-2007, 02:59 AM
...like 7 hours. I can't figure it out. Could someone please help me with this? Thanks!

jscheuer1
08-26-2007, 03:56 PM
function disableForm(theform) {
if (document.forms) {
for (var i = 0,tempobj = theform.elements; i < tempobj.length; i++)
if (tempobj[i].type&&tempobj[i].type.toLowerCase() == "submit")
tempobj[i].disabled = 1;
}
}

CraigH
08-26-2007, 08:12 PM
Hi John,

I tried it but it didnt work. The form submits, the submit button is disabled for a fraction of a second, and then the form refreshes like it's the first time it's been called. Any ideas?

Thanks,
Craig

jscheuer1
08-26-2007, 09:03 PM
Actually, that shows that the function works. The reason it is only temporary is because submitting the form reloads the page. Javascript cannot remember from page load to page load without cookies and cookies enabled on the client side - feasible and effective for many (not nearly all) users - not a direction I think you would want to go.

If you were using get instead of post, you could (in the head):


<script type="text/javascript">
function getQval(n, m) {
/*my n=name, m=searchString(optional)*/
if(!arguments[0]||typeof n!='string')
return null;
var r=new RegExp('[?&;]'+n+'=([^&;#]*)'), m=arguments[1]?m:location.search;
return (m=r.exec(m))? unescape(m[1]) : null;
}

function disableForm(theform) {
if (document.forms) {
for (var i = 0,tempobj = theform.elements; i < tempobj.length; i++)
if (tempobj[i].type&&tempobj[i].type.toLowerCase() == "submit")
tempobj[i].disabled = 1;
}
}
</script>

And, right before the closing body tag:


<script type="text/javascript">
if(document.forms&&getQval('submitp'))
disableForm(document.forms['addjob3'])
</script>

This still requires that the user has javascript enabled, better odds than cookies, but still not 100%.

Ultimately though, since it appears that you have a PHP enabled server and are using the post method, there should be a way to do this all on the server side.

Since I am not personally well versed in PHP, I'd suggest that you take this up in the PHP forum. There should be a way to take the mere existence of the post data and translate that into serving the form with the submit button already disabled.

CraigH
08-26-2007, 11:10 PM
I really apreciate your help John. You provided some insights I was unaware of. I'll try the PHP forum.

Thanks again,
Craig