enable/disable submit button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andersond
    New Member
    • Feb 2007
    • 110

    enable/disable submit button

    I need to enable or disable a submit button based on the value of a data field stored in a database. I have found working examples of how to enable or disable based on a checkbox; but, I need it to work based on a data field value. I have created a textbox in a hidden area that contains the stored data value. I have set the beginning value of the submit button to disabled. I want the submit button to be enabled if the value in the textbox is "YES" and to remain disabled if the value is "NO". The textbox name is "vehiclesAccura te"; and, the name of the submit button is "B1" and the id is "b1".

    The submit button is now disabled. What script code will I need to make it enabled if the value of vehiclesAccurat e="YES"?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You can enable a submit button with JavaScript by setting its disabled property to false, but the question is why would you want to in this case, when you can just use the server-side code. During page load, if the server-side code is setting the hidden field, it can also enable/disable the submit button based on that value.

    Comment

    • andersond
      New Member
      • Feb 2007
      • 110

      #3
      How do you enable/disable a submit button with server-side code? If it's too much trouble to enable or disable with javascript I could just make it visible or hidden; but, I would rather disable or enable so the user can see that there is a submit button present.

      The default state is disabled. If they have indicated that a certain field value (a VIN number on a tractor) is accurate on a previous page I want to enable the submit button so they can continue. Otherwise I want them to go back and verify that it is accurate. They have the option of putting their submission on hold and coming back at any time in the next 30 days to enter accurate information and verify its accuracy.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        As I said, you can enable/disable with JavaScript and it's not much trouble at all.

        To disable using server-side code, use an if statement, e.g.
        Code:
        <input type="submit" ... <?php if (/* condition not met */) echo "disabled='disabled'" ?>">
        (example for PHP, change for whichever language you're using).

        Comment

        • andersond
          New Member
          • Feb 2007
          • 110

          #5
          I appreciate your expertise in php but I really need you to answer my question. This code does not work. What changes can I make to get this to work using javascript?

          Code:
          function init(){
          	if(document.all.VehiclesAccurate.value=="YES" &&
                             document.all.DriversAccurate=="YES"){
          	document.all.b1.disabled=false;
                          } else {
                          document.all.b1.disabled=true;
          	document.getElementById('tableVIN_DLmsg').swapNode
                          (document.getElementById('swapTable2'));
          	document.getElementById("tableVIN_DLmsg").style.visibility="visible";
          		document.getElementById("swapTable2").style.visibility="hidden";
          }
          }
          Code:
          <input type="submit" value="Request to BIND" name="B1" id="b1" disabled="disabled" style="font-family: Tahoma; font-size: 8pt; font-weight: bold" tabindex="1">

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            ...and I've been telling you that you can do it in JavaScript, but it'd make more sense on the server-side. However, if you insist...

            document.all is non-cross-browser/non-standard. Use document.getEle mentById() instead:
            Code:
            if(document.getElementById("VehiclesAccurate").value=="YES" &&
                               document.getElementById("DriversAccurate").value=="YES") {
            	document.getElementById("b1").disabled=false;

            Comment

            Working...