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
    Oct 2011
    Location
    Dallas, Texas
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Switch form action after php validation

    I need for the submission of an unvalidated from to reload the page to allow for php validation which, if passed, would use javascript to change the form's action to an external site.

    First, is the following approach sound?
    Code:
    <script type="text/javascript">
    	if(my_php_validation_ok == true)
    		document.form.action="the_external_url"
    	else
    		document.form.action="current_page.php"
    </script>
    Second, how should the <form> action be styled to accomplish this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    That ought to work
    Assumes that my_php_validation_ok is passed from PHP.

    Not sure what you mean by "how should the <form> action be styled to accomplish this?". Presumably the validation is invoked with a submit button.

    Something like this:-

    Code:
    <script type = "text/javascript">
    
    function OnSubmitForm() {
    if(my_php_validation_ok) {
    document.myform.action="the_external_url";
    }
    else {
    document.myform.action="current_page.php";
    }
    alert (document.myform.action);  // for testing
    }
    </script>
    
    
    <form name="myform" onSubmit="return OnSubmitForm();">
    <input type = "submit" name = "submitButton" value = "Submit">
    </form>

    Quizmaster: King Edward VIII abdicated the throne in order to marry whom?
    Contestant: Anne Boleyn

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    ProTek2 (10-08-2011)

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Location
    Dallas, Texas
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The following line of your answer was the second part of what I was asking:
    Code:
    <form name="myform" onSubmit="return OnSubmitForm();">
    Thank you for your assistance.


  •  

    Posting Permissions

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