php
  • ajax
  • 2016-06-30 13 views 0 likes 
    0

    發送響應回頁面我想知道這是可能發送迴應通過使用一些if/else響應我們從服務器獲得成功在$ .ajax ..這是可能通過做一些if/else在ajax

    AJAX

    $.ajax({ 
    
        url : "request/register.php", 
        type : "POST", 
        data : 'firstName='+firstName + '&lastName='+lastName + '&userName='+userName + '&email='+email + '&password='+password + '&con_password='+con_password, 
        dataType : "text", 
        beforeSend : function(http){ 
         $('#reg').val("Submitting...."); 
        }, 
        success : function(response,status,http){ 
         var text = response; 
         alert(text); 
         if(text != "<span class=\"error\" data-icon ='&#xea0f;'>Empty Fields</span>"){ 
           alert("Done");    
         }else{ 
           alert("oOps") 
         } 
    
        }, 
        error : function(http,status,error){ 
         alert('server error'); 
        } 
    
    }) 
    

    registeration.php

    //creating a variable for error messages 
    $error = ""; 
    //creating a variable for success messages 
    $success = ""; 
    //form validation 
    if($_SERVER['REQUEST_METHOD'] == "POST"){ 
    
    
        $firstName = trim(filter_input(INPUT_POST, 'firstName' ,FILTER_SANITIZE_STRING)); 
        $lastName  = trim(filter_input(INPUT_POST, 'lastName'  ,FILTER_SANITIZE_STRING)); 
        $userName  = trim(filter_input(INPUT_POST, 'userName'  ,FILTER_SANITIZE_STRING)); 
        $email  = trim(filter_input(INPUT_POST, 'email'  ,FILTER_SANITIZE_EMAIL)); 
        $password  = trim(filter_input(INPUT_POST, 'password'  ,FILTER_SANITIZE_STRING)); 
        $confirm_pass = trim(filter_input(INPUT_POST, 'con_password' ,FILTER_SANITIZE_STRING)); 
    
    
        //checking for empty feilds 
        if($firstName == "" || $lastName == "" || $userName == "" || $email == "" || $password == "" || $confirm_pass == ""){ 
         $error = "Empty Fields"; 
        } 
    
        //checking username length 
    
        if(empty($error) && strlen($userName)<=5){ 
         $error = "Username must be greater than 5 characters"; 
        } 
    
        //checking for username existence 
    
        if(empty($error) && user_exist($userName)){ 
         $error = "Username already exist"; 
        } 
    
        //email validation 
        if(empty($error) && !filter_var($email,FILTER_VALIDATE_EMAIL)){ 
         print_r($_POST); 
         $error = "Invalid Email address"; 
        } 
    
        //checking for email existence 
    
        if(empty($error) && email_exist($email)){ 
         $error = "Email already exist"; 
        } 
    
        //checking password length 
        if(empty($error) && strlen($password)<=8){ 
         $error = "Password must be greater than 8 characters"; 
        } 
    
        //matching confirm password 
        if(empty($error) && $password !== $confirm_pass){ 
         $error = "Password not match"; 
        } 
    
        if(empty($error)){ 
         if(user_registration($firstName,$lastName,$userName,$email,md5($password))){ 
         $success = "Registered Suceessfully"; 
         }else{ 
         $error = "Something went wrong"; 
         } 
        } 
    
    } 
    
        if(!empty($error)){ 
         echo "<span class=\"error\" data-icon ='&#xea0f;'>".$error."</span>"; 
        } 
    
        if (empty($error) && !empty($success)) { 
        echo "<span class=\"success\" data-icon ='&#xea10;'>".$success."</span>"; 
        } 
    

    如果反應是一樣的東西,比我想設置輸入[類型= 「文本」]值應該是相同的哪些用戶類型和輸入[類型=「密碼」 ]應該是空白&在其他情況下,如果響應是類似的東西,我想所有的輸入域都是空的..

    +0

    請顯示您的所有源代碼。 –

    +0

    嘗試'json_encode($ your_post_data)'成功; –

    回答

    1

    是的,這是可能的。但由於機體可能會在更改超時,因此最好在HTTP/1.1 Status Codes上執行if/else邏輯。

    例如;

    在你的錯誤響應,只需使用http_response_code()

    http_response_code(400); 
    

    而在你的成功響應返回錯誤代碼400 Bad Request,簡單地返回一個201 Created

    http_response_code(201); 
    

    現在,在你的jQuery,你可以只需看看退貨狀態代碼併爲最終用戶做適當的事情即可。

    //[...] 
    success : function(response,status,http) { 
        if(http.status === 201) { 
         alert("User created"); 
        } else { 
         //Assume there was an error 
         //Do some error logging for the end-user 
         alert("Could not create the user"); 
        } 
    } 
    
    相關問題