2014-09-24 157 views
1

我想驗證我的表單。所以我創建my_script.js並使用它的表單的onsubmit事件。PHP表單驗證與AJAX

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

但腳本不能正常工作。 uname,telno1是有效的,但其他ifs不起作用。 例如,如果用戶名或telno1爲空,然後單擊submit1按鈕,則在unametxt或telno1txt上寫入javascript工作和錯誤。但如果密碼或其他爲空,並單擊submit1按鈕,表單張貼。 我不明白爲什麼兩個if語句工作,其他人不。

的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])(?=.*[[email protected]#$%^&*])[[email protected]#$%^&*]{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; 

    } 
    }*/ 

回答

3

刪除否則,如果情況 並作出是否病例所以它會檢查所有。

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; 
0

這是因爲只有在沒有電話號碼的情況下才會訪問if(!密碼)(因爲您使用的是其他if)。

因此,它會檢查電話號碼,如果有電話號碼,則不會執行其他任何其他語句。

只要使用case語句,或者至少只使用沒有else的語句。