Results 1 to 6 of 6

Thread: Cant make the submit button disable

  1. #1
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cant make the submit button disable

    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!

  2. #2
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying this for hours...

    ...like 7 hours. I can't figure it out. Could someone please help me with this? Thanks!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    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;
    }
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <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:

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I really apreciate your help John. You provided some insights I was unaware of. I'll try the PHP forum.

    Thanks again,
    Craig

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •