1

I'm using the following code to send POST request if validation passes. But it doesn't. Can someone show me a wway to make it work.

<script type="text/javascript">

    function go()
    {
    var username = document.form.name.value;
    var pwd = document.form.pwd.value;
    if (username == '') {
      var oldHTML = document.getElementById('myAnchor').innerHTML="Please fill out username.";
    var newHTML = "<span style='color:red'>" + oldHTML + "</span>";
    document.getElementById('myAnchor').innerHTML = newHTML;
    }
    if (pwd == '') {
      var oldHTML = document.getElementById('myAnchor2').innerHTML="Please fill out password.";
    var newHTML = "<span style='color:red'>" + oldHTML + "</span>";
    document.getElementById('myAnchor2').innerHTML = newHTML;
    }
      else {
       document.form.method="POST";
       document.form.action = "data.php";
    }
    </script>

2 Answers 2

1

Everything looks correct, you just need to actually submit the form. Add the following line into your else block like this:

else {
    document.form.method="POST";
    document.form.action = "data.php";
    //submit the form -- new line is below
    document.form.submit();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

below is snippet how to submit form via javascript

<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
<a href="javascript: submitform()">Search</a>
</form>
<script type="text/javascript">
function submitform()
{
  document.myform.submit();
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.