Using javascript and php together -


I have a PHP form which requires some very simple verification to be submitted. I would like to do the verification client instead, because there is a bit of server-side verification that is to deal with writing the value of the database. So I want to call a JavaScript function onsmit to compare values ​​in just two password areas. What I have it:

  function is valid (form) {var password = form.password.value; Var password2 = form.password2.value; Warning ("password:" + password + "password2:" + password2); If (password! = Password2) {warning ("not equal"); Document.getElementById ("passwordError") style.display = "inline". return false; } Warning ("par"); Back true; }  

It is being thought that there is a default-hidden device with an error message, if two passwords do not match. Alerts are for displaying only the values ​​of the password and password 2, and then to indicate whether they match or not (will not be used in the production code).

I am using an input type = submit button, and I am calling the function as a tag:

  & lt; Form action = "& lt ;? Php echo $ _SERVER ['PHP_SELF'];? & Gt;" method = "post" onsubmit = "return valid (this);" & Gt;  

When entering non-matching values, everything is expected. I hope that (and will be estimated, based on past usage) that if the function has been made false, then the actual submission will not be. And yet, it is. I'm testing by entering non-matching values ​​in the password field, and the alert clearly shows me that there are no value and equal results, but the actual form action is still going on and it's going to be written in my database Is trying.

I'm new to PHP; Is there anything about this that will not let me combine with JavaScript like this? Would it be better to use an input type = button and submit it to the function () if it gets right?

If there is an error in javascript, the refund has not been lost and the form will be submitted anyway .

And your javascript has an error: getElementByID should be getElementById .


Comments