简体   繁体   English

防止在验证失败的情况下引导程序模式消失

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

I'm trying to stop bootstrap modal from disappearing in case there is any invalid data entered by user. 我正在尝试阻止引导模式消失,以防用户输入任何无效数据。

my php validation code is: 我的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;        

}

after the validation the php should return a flag in case of validation failure. 验证后,如果验证失败,php应该返回一个标志。 The below script should read the flag and retrieve the modal, including the data previously entered by the user: 以下脚本应读取标志并检索模式,包括用户先前输入的数据:

<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');
    }
});

this obviously did not work. 这显然没有用。 Any suggestions? 有什么建议么?

Server-side code 服务器端代码

<?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;    
}
?>

Client-side code 客户端代码

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

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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