2

I'm new with PHP and JavaScript. I eant to validate form, and if the form input fields are correct, then send it to php external script. I've tried something like this:

<head>
<script type="text/javascript">
  function Validate(){
    var x=document.forms["form"]["firstname"].value;
    var y=document.forms["form"]["lastname"].value;
    var z=document.forms["form"]["age"].value;
    if(x==null || x=="") {
      alert("Input name!");
    } else if(y==null || y=="") {
      alert("Input surname!");
    } else if(z==null || z=="") {
      alert("Input age!");
    } else {
  // IS IT POSSIBLE TO SEND FORM HERE FORM VARIABLES TO PHP SCRIPT?
    }
  }
</script>
</head>
<body>

  <form name="forma" action="validate.php" method="post" onSubmit="return Validate()">
    Firstname: <input type="text" name="firstname">
    Lastname: <input type="text" name="lastname">
    Age: <input type="text" name="age">
    <input type="submit">
  </form>
</body>
</html>
2
  • after your alert enter: return false;. This way the form will not be submitted. Also make sure to check the variables again in your php script. Commented Mar 19, 2013 at 22:22
  • You can skip those null checks Commented Mar 19, 2013 at 22:26

4 Answers 4

1

Since you are not returning false from your function, the form should be being submitted anyway. You can stop it from being submitted by having your validate() function return false.

Sign up to request clarification or add additional context in comments.

1 Comment

ahaa i forgot to write return false. it works ok now, thanks a lot :)
1

Your code looks correct, other than the fact you probably want to return false on failure to prevent the form from submitting. When not returning false, the form will simply continue to submit.

I also think you probably meant to name your form form not forma

1 Comment

ahaa i forgot to write return false. it works ok now, thanks a lot :)
1

You should validate the input server-side (php), even if you did it client-side (javascript).

You do this by checking on $_POST in validate.php:
Quite at the beginning of your script:

if (isset($_POST)) {  // form has been sent

    if (!isset($_POST['firstname']) || strlen($_POST['firstname'])==0) { // no entry 

        $e['firstname']='please enter a firstname!'; // prepare error-msg
    }

    if (!isset($e)) {  // no error, continue handling the data

        // write data to database etc.
    }
}

// in your form...

if (isset($e)) echo "there were errors: ". implode(',',$e);

Comments

1

You need to put this code:

function Validate(){
var x=document.forms["form"]["firstname"].value;
var y=document.forms["form"]["lastname"].value;
var z=document.forms["form"]["age"].value;
if(x==null || x=="") {
  alert("Input name!");
  return false;
} else if(y==null || y=="") {
  alert("Input surname!");
  return false;
} else if(z==null || z=="") {
  alert("Input age!");
  return false;
} else {
  return true;
}
}

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.