0

PHP Code:

$dom = new DOMDocument;
$headtitle = "Register";
$errors = array();
if(isset($_POST['register'])){
    $username = preg_replace('/[^A-Za-z]/', '', $_POST['username']);
    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $c_password = $_POST['c_password'];
    $birthday = $_POST['birthday'];
    $country = $_POST['country'];
    $gender = $_POST['gender'];
    $age = $_POST['age'];
    $level = $_POST['level'];
    $date = $_POST['date'];

    if(file_exists('users/' . $username . '.xml')){
        $errors[] = '  Username already exists';
    }
    if($username == ''){
        $errors[] = '  Username is missing. Try again.';
    }
    if($name == ''){
        $errors[] = '  Name is missing. Try again.';
    }
    if($lastname == ''){
        $errors[] = '  Lastname is missing. Try again.';
    }
    if($country == ''){
        $errors[] = '  Country is missing. Try again.';
    }
    if($gender == ''){
        $errors[] = '  Gender is missing. Try again.';
    }
    if($age == ''){
        $errors[] = '  Age is missing. Try again.';
    }
    if($email == ''){
        $errors[] = '  Email is missing. Try again.';
    }
    if($password == '' || $c_password == ''){
        $errors[] = '  Passwords are missing. Try again.';
    }
    if($password != $c_password){
        $errors[] = '  Passwords do not match';
    }
    if(count($errors) == 0){
        $xml = new SimpleXMLElement('<user></user>');
        $xml->addChild('name', ($name));
        $xml->addChild('lastname', ($lastname));
        $xml->addChild('password', md5($password));
        $xml->addChild('birthday', $birthday);
        $xml->addChild('country', $country);
        $xml->addChild('gender', $gender);
        $xml->addChild('age', $age);
        $xml->addChild('email', $email);
        $xml->addChild('level', $level);
        $xml->addChild('date', $date);
        $xml->asXML('users/' . $username . '.xml');
        header('Location: index.php');
        die;
    }
}

Javascript Code:

function vaildate() {
    if (document.getElementById('username').value.length <= 4) {
        document.getElementById('errors').innerHTML = "Username must me more than 4 words <br />";
        return false;
    }
    return true;
}

Now my problem is, that when I click submit button (that contains name="login" and onclick="vaildate();") he excute only php errors and ignores javascript errors (assuming that id="username" has less than 4 words).

My question is how can I make Javascript & PHP errors work? not only PHP and the system ignores Javascript.

Thank you all..

EDIT:

Also i got this code to echo PHP errors

if(count($errors) > 0){
  echo '<font color="red"><ul>';
   foreach($errors as $e){
    echo '<li>' . $e . '</li>';
   }
  echo '</ul></font>';
}
4
  • 1
    I hope nobody chooses a username with something nasty in it e.g. ../../ Commented Jun 19, 2013 at 20:09
  • You not really helping with that hope of yours.. Commented Jun 19, 2013 at 20:10
  • 1
    I'm trying to point out that your code has flaws in it that leave it very vulnerable to basic injection attacks. In regards to your actual question I was writing a response Commented Jun 19, 2013 at 20:12
  • I awere that, some people already said that to me. But I don't mind since I not gonna publish the website anyway... it just for experiences. Commented Jun 19, 2013 at 20:14

2 Answers 2

1

Try this:

onclick="return vaildate();"

You need to return the validate function (return the true or false), not just call it.

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

Comments

0

Your Javascript and PHP you are showing looks fine. What we don't have is the actual markup of the login page. My suspicion is that your markup is not consistent with the code you have in your Javascript.

If you could also try and explain more specifically what you mean by

My question is how can I make Javascript & PHP errors work? not only PHP and the system ignores Javascript.

Have you used a Javascript debugger to see if part your Javascript (maybe elsewhere on the page) is erroring?

1 Comment

I've, the code isn't really the problem. all i want is to make Javascript stop submiting (incase there error) the code and make new user.

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.