Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript, PHP, dynamic validation and Submit Button

    Hello all. I have an issue which I'm trying to tackle but I can't find which way would be best.

    I have the following function in a Javascript file:
    Code:
    function validate(objName, fieldName, objValue, objValue2)
    {
    var objOut = document.getElementById('msg_' + fieldName);
    var params = "";
    
    if(objName == "field_length")
    {
      params = "?field_length=" + objValue;
    }
    else if(objName == "postal_code")
    {
      params = "?postal_code=" + objValue;
    }
    else if(objName == "phone_number")
    {
      params = "?phone_number=" + objValue;
    }
    else if(objName == "email")
    {
      params = "?email=" + objValue;
    }
    else if(objName == "afm")
    {
      params = "?afm=" + objValue;
    }
    else if(objName == "field_length_no_require")
    { 
      params = "?field_length_no_require=" + objValue;
    }
    else if(objName == "field_selected")
    { 
      params = "?field_selected=" + objValue;
    }
    
    XMLHttp.open('GET', './form_valid.php' + params, true);
    XMLHttp.onreadystatechange = function()
    {
    	if(XMLHttp.readyState == 4 && XMLHttp.status == 200)
    	{
    		objOut.innerHTML = XMLHttp.responseText;
    	}
    }
    XMLHttp.send(null);
    }
    and then each function in the php file returns through echo a string which is to be displayed as you can see in innerHtml.

    The problem is that I want the Submit button to be disabled only until all fields are valid at which case it will be enabled.

    I have thought of returning a variable from PHP and storing it in Javascript to use it in a big if statement but I can't return any variables from what I've searched.

    Maybe I'm going about this whole thing wrong. I'd like to avoid validating in Javascript.

    Is there any other way to accomplish this?

    Or any ideas in general to get what I want out of this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation simply by disabling JavaScript.

    Having said that, you are wanting something like:-

    Code:
    <input type = "submit" name = "subt" id = "subt" value = "Submit Form" disabled>
    
    <script type = "text/javascript">
    
    function validate () {
    
    // validation here, and if validation passed:-
    
    document.getElementById("subt").disabled = false;
    }
    </script>
    Obviously if the submit button is disabled it cannot call the validate function.

    It is better to keep the submit button enabled, and then onclick = return validate(this.form). Then the function validate() returns false or true as appropriate.


    “In ancient times they had no statistics, so they had to fall back on lies” — Stephen B Leacock
    Last edited by Philip M; 12-09-2008 at 03:45 PM.

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, thanks for your help. Indeed it's not safe to use Javascript to validate but the only thing I have Javascript doing is calling my PHP validation page which contains all the validation. However, you're right, if Javascript gets disabled, no validation would ever be done.

    Now if validation was done on JS, then yea I could just call the disabled property but it's being done in a PHP file, server-side. The only thing JS has info on is the name of the field that gets validated, it's value and the "echo" returned from PHP page. Someone told me, I could use an array, then depending on the "echo" string I set the array to the field name, and whether it's valid or not. It adds a lot of code and iterations so I don't know if it's really a good idea or not. Or if it'll work for that matter..


  •  

    Posting Permissions

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