Rahul
Rahul

Reputation: 1639

how to disable a submit button on clicking and submit the form data to perl script?

i have a html form. when the submit button is pressed, a perl script is called and the form data is submitted to the perl script. the form contains a text box and a submit button. the submit button also validates whether the text box contains some value or not. and if it contains ot submits the form else it alerts a message. below is the sample code:

<html>
        <head>
                <title>files move</title>

                <script type="text/javascript">

                  function check_filename()
                  {
                                if (!frm1.FileName.value)
                                {
                                        alert ("No File is selected to Move");
                                        return false;
                                }
                                else
                          {
                                        document.getElementById("btn_sbmt").disabled = 'true'
                                        return true;
                          }
                  }
                </script>
        </head>

    <body>

                <form id="frm1" name="frm1" action="/cgi-bin/sample_move.pl" method="GET">

            <input type="text" name="FileName" id="FileName">
                        <input type="submit" name="btn_sbmt" id="btn_sbmt" value="MOVE FILES" onclick="return check_filename();">

                </form>
        </body>
</html> 

when i click on the submit button, i want to 1st check whether the text box contain some value or not. If it contains then i want to call the perl script and disable the submit button to avoid clicking the submit button multiple times. the above code checks whether the text box is empty or not and if it is not empty then it disables the submit button but it does not submit the form to perl script. can anyone tell how to do this?

Upvotes: 1

Views: 1179

Answers (2)

Man Programmer
Man Programmer

Reputation: 5356

add this line in your else part

document.getElementById("frm").submit();

Upvotes: 0

Sithu
Sithu

Reputation: 4421

This sample is based on your example.

function check_filename()
{
    if (!frm1.FileName.value)
    {
        alert ("No File is selected to Move");
        return false;
    }
    else
    {
        document.getElementById("btn_sbmt").disabled = 'true';
        document.getElementById("frm1").submit();
        return true;
     }
}

For a cleaner version, you can use jQuery. http://api.jquery.com/submit/

Upvotes: 1

Related Questions