0

Hopefully this wont be quite as simple as I have been thinking it should have been. A simple HTML forum when submit is clicked calls a Javascript for validation to be handled by PHP to put the information that has been validated into a MySql table. The issue I am having is once i get the javascript return true, how do I pull this out to use in PHP?

The Forum:

<form action="" method="post" name="register">
<div class="field">
    <label for "username">Username</label>
    <input type="text" name="username" id="username" value="" autocomplete="off">
</div>

<div class="field">
    <label for "password">Choose a password</label>
    <input type="password" name="password" id="password">
</div>

<div class="field">
    <label for "password_again">Reenter Password</label>
    <input type="password" name="password_again" id="password_again">
</div>

<div class="field">
    <label for "name">Name</label>
    <input type="text" name="name" id="name" value="" autocomplete="off">
</div>
<input type="submit" value="Register" id="submit" onclick="validate()" />

The external JS file:

function validate() {
var password = document.register.password.value;
var passwordagain = document.register.password_again.value;
var name = document.register.username.value;
var test = new RegExp("[^a-zA-Z0-9]");
var space = String.fromCharCode(32);
if (name.match(test)) {
    alert("Your name may only contain letters and numbers.");
    return false;
} else if (name.match(space)) {
    alert("Spaces are not allowed.");
    return false;
} else if ((name.length <= 2) || (name.length >= 11)) {
    alert("Name must be between 3 and 10 characters.");
    return false;
} else if (!password.match(passwordagain)) {
    alert("Passwords do not match");
    return false;
} else if ((password.length <= 6) || (password.length >= 32)) {
    alert("Password must be between 6 and 32 characters long.");
    return false;
}
return true;
}
Krevex
  • 3
  • 1
  • Whatever your form action points to will be where the data is submitted. If blank, it will be submitted to the page that the browser is on. You can either add PHP code to handle a POST in your current page, or create a dedicated PHP page that handles the POST data and change the form action. – gcochard Mar 30 '14 at 02:33

1 Answers1

0

You can set the form's action attribute to point to a PHP page to be redirected to after the form has been validated.

Alternatively, you can use an XMLHttpRequest to send the form data to a PHP script. There was already a question asked on how to do this which you can find here.


Side Note:

It is usually a good idea to do all form validation server-side. This is because it is very easy for someone to hack JavaScript. Anyone could change your validate() function by opening up their browser's console and doing something like this:

validate = function() { return true; };

Community
  • 1
  • 1
NathanW
  • 302
  • 2
  • 9
  • Thanks Nathan and Greg. I'll spend some time and see if I can manage to get a php equivalent of the JS i slapped together. Did not realize it'd be that easy to circumnavigate. – Krevex Mar 30 '14 at 03:03