简体   繁体   中英

PHP form validation with ajax

i want to do validation my form. and so i create my_script.js and use it form's onsubmit event.

<form id="form1" name ="form1" method="POST" action="index.php" onSubmit="return validatePassword()">

But script not work exactly. uname, telno1 is validate but other else ifs not work. For example if username or telno1 is null and click submit1 button, javascript work and error write on unametxt or telno1txt. but if password or other is null and click submit1 button, form posted. i dont understand why two if statement work, others dont.

index.php

    <!DOCTYPE html>
<html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> 
<html class="lt-ie9 lt-ie8" lang="en">
<html class="lt-ie9" lang="en"> 
<html lang="en"> 
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>TekSifre Değiştirme Ekranı</title>
  <link rel="stylesheet" href="css/style.css">
  <!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->

<script type="text/javascript" src="script/my_script.js"></script>
</head>
<body>
  <section class="container">
    <div class="login">
      <h1>TekŞifre Yenileme Ekranı</h1>


        <div>Kullanıcı Tipini Seçiniz:</div>
        <input type="radio" onclick="if(this.checked){myFunction()}" name="tip" value="personel"/>Personel
        <input type="radio" onclick="if(this.checked){myFunction1()}" name="tip" value="ogrenci"/>Öğrenci

        <form id="form1" name ="form1" method="POST" action="index.php" onSubmit="return validatePassword()">

        <p><input type="text" name="uname" id="uname" value="" placeholder="Kullanıcı Adınız" style="width:130px;">
          <input type="label" id="ogrenci" name="ogrenci" readonly=true value="" style="width:130px;">
          <span id="unametxt" name="unametxt" class="required"></span></p>

        <p><input type="text" id="telno1" name="telno1" value="" placeholder="Cep : 5xx xxx xx xx" style="width:150px;" maxlength="10" >
          <span id="telno1txt" name="telno1txt" class="required"></span></p>

       <!-- <p><input type="text" id="telno2" name="telno2" value="" placeholder="xxx xxxx" style="width:180px;" maxlength="7">
           <span id="telno2txt" name="telno2txt" class="required"></span></p>-->

        <p><input type="password" id="password" name="password" value="" placeholder="Eski Şifrenizi Giriniz">
          <span id="passwordtxt" name="passwordtxt" class="required"></span></p>

        <p><input type="password" id="newPassword" name="newPassword" value="" placeholder="Yeni Bir Şifre Giriniz">
          <span id="newPasswordtxt" name="newPasswordtxt" class="required"></span></p>

        <p><input type="password" id="new2Password" name="new2Password" value="" placeholder="Yeni Şifreyi Tekrar Giriniz">
          <span id="new2Passwordtxt" name="new2Passwordtxt" class="required"></span></p>

        <p><input type="submit" id="Submit1" name="Submit1" value="Değiştir" ></p>
      <!--<td><input type="submit" name="Submit1" value="Değiştir" class="button" onclick="validatePassword()"></td> -->
      </form>
    </div>
  </section>

  <section class="about">
    <p class="about-links">
      <a href="http://bim.kastamonu.edu.tr/index.php/tr/menu-tek-sifre-tr.html" target="_blank">Tek Şifre Nedir</a>

    </p>
    <p class="about-author">
      &copy; 2014&ndash;2014 <a href="http://bim.kastamonu.edu.tr" target="_blank"></a>Kastamonu Üniversitesi<br>
     <a href="http://bim.kastamonu.edu.tr" target="_blank">Bilgi İşlem Daire Başkanlığı
  </section>
</body>
</html>
<?php

if (isset($_POST['Submit1'])) {

    $uname=$_POST['uname'];
    $telno1=$_POST['telno1'];
   // $telno2=$_POST['telno2'];
    $password=$_POST['password'];
    $newPassword=$_POST['newPassword'];
    $new2Password=$_POST['new2Password'];



      csvyaz($uname,$password,$newPassword);
      telefoncsvyaz($uname,$telno1,$telno2);
      powershellverify($uname,$password,$newPassword);
      $varmi=dosyayikontrolet($uname);
      if ($varmi)
      {
         powershelltelephone();
        // $a= powershelltelephone();
        // echo "<pre>";print_r($a);exit;
          powershellreset();
          telefonlarcsv($uname,$telno1,$telno2);
          $silmi=dosyayisil($uname);

          if($silmi){
              echo "<script>
                alert('Şifreniz Başarıyla Değiştirildi.');
                window.location.href='index.php';
                </script>";}
          else{
              echo "<script>
                alert('Hata Oluştu.');
                window.location.href='index.php';
                </script>";}        
      }else{
          echo "<script>
            alert('Kullanıcı Adı yada Şifrede Hata.');
            window.location.href='index.php';
            </script>";
      }
}

function csvyaz($uname,$password,$newPassword)
{
  $data = array("samaccountname,oldpassword,newpassword",
                  $uname.",".$password.",".$newPassword);
  $fp = fopen('C:\Reset\PasswordReset.csv', 'w');
  foreach ( $data as $line ) {
      $val = explode(",", $line);
      fputcsv($fp, $val);
  }
  fclose($fp);
}

function telefoncsvyaz($uname,$telno1,$telno2)
{
  $tel="+90".$telno1.$telno2;
    $data = array("samaccountname,mobilephone",
                  $uname.",".$tel);
  $fp = fopen('C:\Reset\TelReset.csv', 'w');
  foreach ( $data as $line ) {
      $val = explode(",", $line);
      fputcsv($fp, $val);
  }
  fclose($fp);
}

function telefonlarcsv($uname,$telno1,$telno2)
{
  $varmi=file_exists("C:/Reset/Telefonlar.csv");

  if ($varmi){
    $data = array($uname.",".$telno1.",".$telno2);
  }
  else{
      $data = array("samaccountname,telno1,telno2",$uname.",".$telno1.",".$telno2);
  }

  $fp = fopen('C:\Reset\Telefonlar.csv', 'a');
  foreach ( $data as $line ) {
      $val = explode(",", $line);
      fputcsv($fp, $val);
  }
  fclose($fp);
}

function powershellverify($uname,$password,$newPassword)
{
  $myfile = fopen("c:/Reset/".$uname.".txt", "w");
 // $output2 = shell_exec('powershell.exe -c "C:\wwwroot\logoncss\8-login-form\PasswordVerify.ps1"');
 // return $output2;
  return $myfile;
}
function powershellreset()
{
  $output2 = shell_exec('powershell.exe -c "C:\wwwroot\logoncss\8-login-form\PasswordReset.ps1"');

 // return $output2;
}
function powershelltelephone()
{
  $psPath = 'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned c:\wwwroot\logoncss\8-login-form\Telephone.ps1';
  $output2 =shell_exec($psPath);
 // $output2 =exec('powershell.exe "C:\wwwroot\logoncss\8-login-form\Telephone.ps1"');
 return $output2;
}
function dosyayikontrolet($uname)
{
  $varmi=file_exists("C:/Reset/".$uname.".txt");
  return $varmi;
}
function dosyayisil($uname)
{
  $silmi=unlink("C:/Reset/".$uname.".txt");
  return $silmi;
}



?> 

my_script.js

function validatePassword() {
  var output = true;
  //var formdata = jQuery( "#form1" ).serialize();


  username = document.form1.uname;
  telno1 = document.form1.telno1;
 // telno2 = document.form1.telno2;
  password = document.form1.password;
  newPassword = document.form1.newPassword;
  new2Password = document.form1.new2Password;
  tip = document.form1.tip;


  if(!username.value) {
    username.focus();
    document.getElementById("unametxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
  else if(!telno1.value) {
   // telno1.focus();
    document.getElementById("telno1txt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
  else if((telno1.value)){
      var paswd=  /^(?=.*\d)(?=.*[0-9])(?!.*\s)/;
        if((!telno1.value.match(paswd))) 
        { 
          //  telno1.focus();
            document.getElementById("telno1txt").innerHTML = "Kod kısmında hata";
            output=false;
        }
  }
  else if(!password.value) {
   // password.focus();
    document.getElementById("passwordtxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
  else if(!newPassword.value) {
   // newPassword.focus();
    document.getElementById("newPasswordtxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
  else if(!new2Password.value) {
    new2Password.focus();
    document.getElementById("new2Passwordtxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
  else if(newPassword.value!=new2Password.value) {
      newPassword.focus();
      //newPassword.value="";
      //confirmPassword.value="";
      document.getElementById("new2Passwordtxt").innerHTML = "Yeni şifreler uyuşmadı.";
      output = false;
  } 
   /* else if(!telno2.value) {
    telno2.focus();
    document.getElementById("telno2txt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
  else if((telno2.value)){
       var paswd2=  /^(?=.*\d)(?=.*[0-9])(?!.*\s)/;
        if((!telno2.value.match(paswd2))) 
        { 
            telno2.focus();
            document.getElementById("telno2txt").innerHTML = "Telefonda hata";
            output=false;
        }
  }*/  

  return output;
}

  function myFunction() {
    document.getElementById("ogrenci").value="@kastamonu.edu.tr";
  }
  function myFunction1() {
    document.getElementById("ogrenci").value="@ogr.kastamonu.edu.tr";
  }
  /*function simdikisifre() {
    password = document.form1.password;

        alert(password.value );
      //var paswd=  /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?!.*\s).{5,10}$/;
      var paswd=  /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$/;
       if(!password.value.match(paswd)) 
        { 
            alert('Şifreniz 7 ile 15 karakter arasında bir uzunlukta olmalı.En az bir sayısal ve özel karakter içermelidir.')
            output=false;

    }
  }*/

Remove else if case and make if cases so it will check all.

if(!username.value) {
    username.focus();
    document.getElementById("unametxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
if(!telno1.value) {
   // telno1.focus();
    document.getElementById("telno1txt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
if((telno1.value)){
      var paswd=  /^(?=.*\d)(?=.*[0-9])(?!.*\s)/;
        if((!telno1.value.match(paswd))) 
        { 
          //  telno1.focus();
            document.getElementById("telno1txt").innerHTML = "Kod kısmında hata";
            output=false;
        }
  }
if(!password.value) {
   // password.focus();
    document.getElementById("passwordtxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
if(!newPassword.value) {
   // newPassword.focus();
    document.getElementById("newPasswordtxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
if(!new2Password.value) {
    new2Password.focus();
    document.getElementById("new2Passwordtxt").innerHTML = "Boş bırakılamaz.";
    output = false;
  }
if(newPassword.value!=new2Password.value) {
      newPassword.focus();
      //newPassword.value="";
      //confirmPassword.value="";
      document.getElementById("new2Passwordtxt").innerHTML = "Yeni şifreler uyuşmadı.";
      output = false;
  }  
  return output;

That's because your if(!password) is only visited if there is no phone number (because you are using else if).

So what it does is that it will check the phone number, and if there is one, it won't do any of the other if statements.

Just use a case statement, or at least use only if statements without the else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM