簡體   English   中英

防止在驗證失敗的情況下引導程序模式消失

[英]preventing bootstrap modal from disappearing in case validation failure

我正在嘗試阻止引導模式消失,以防用戶輸入任何無效數據。

我的PHP驗證代碼是:

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

    $conn = new mysqli("localhost", "ahmesmat", "ZainMalek3110", "SignUpsIroners");

    if ($conn->connect_error) {

            die("Connection failed: " . $conn->connect_error);

    } else
        if (!$_POST['lname'])
            $error="</br>Your first name.";

        if (!$_POST['fname'])
            $error.="</br>Your last name.";

        if (!$_POST['email']) 
            $error.="</br>Your email address.";

        if (!(isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])))
            $error.="</br>Your full date of birth.";

        if (!$_POST['phone']) 
            $error.="</br>Your phone number.";

        if (!$_POST['ssn']) 
            $error.="</br>Your social security number.";

        if (!$_POST['staddress']) 
            $error.="</br>Your street address.";

        if (!$_POST['city']) 
            $error.="</br>Your city.";

        if (!$_POST['state']) 
            $error.="</br>Your state.";

        if (!$_POST['zcode']) 
            $error.="</br>Please enter your zip code.";

        if (!$_POST['country']) 
            $error.="</br>Your country.";

        if (!$_POST['radio']) 
            $error.="</br>Tell us if you have an iron or planning to get one.";

}
if (isset($error)) {
    $flag=1;        

}

驗證后,如果驗證失敗,php應該返回一個標志。 以下腳本應讀取標志並檢索模式,包括用戶先前輸入的數據:

<script>
//this will launch the modal the first time
$(window).load(function(){
    $('#myModal').modal('show');
});

//this was suppose to retrieve the modal    
$.ajax({
    url:"signupstore.php"
}).done(function() {    
    var flag='<?php echo json.encode($flag); ?>';

    if (flag==1) {
        $('#myModal').modal('show');
    }
});

這顯然沒有用。 有什么建議么?

服務器端代碼

<?php
$error = '';
if (isset($_POST['submit'])) {
    $conn = new mysqli("localhost", "ahmesmat", "ZainMalek3110", "SignUpsIroners");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }else{
        if (!$_POST['lname'])
            $error="</br>Your first name.";

        if (!$_POST['fname'])
            $error.="</br>Your last name.";

        if (!$_POST['email']) 
            $error.="</br>Your email address.";

        if (!(isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])))
            $error.="</br>Your full date of birth.";

        if (!$_POST['phone']) 
            $error.="</br>Your phone number.";

        if (!$_POST['ssn']) 
            $error.="</br>Your social security number.";

        if (!$_POST['staddress']) 
            $error.="</br>Your street address.";

        if (!$_POST['city']) 
            $error.="</br>Your city.";

        if (!$_POST['state']) 
            $error.="</br>Your state.";

        if (!$_POST['zcode']) 
            $error.="</br>Please enter your zip code.";

        if (!$_POST['country']) 
            $error.="</br>Your country.";

        if (!$_POST['radio']) 
            $error.="</br>Tell us if you have an iron or planning to get one.";
    }
}
if ($error != '') {
    echo $error;   
}else{
    return true;    
}
?>

客戶端代碼

<script>
//this will launch the modal the first time
$(document).ready(function(){
    $('#myModal').modal('show');
    $.ajax({
      url: 'signupstore.php',
      type: 'POST',
      data: {email: 'your_email', country: 'your country'}, // pass your data here
      success: function(data) {
        //called when successful
        if(data != ''){
            $('.error').html(data); // add a div to display the return errors
        }else{
            $('#myModal').modal('hide');
        }
      }
    });    
});
</script>

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM