dssqq64884 2015-12-23 05:02
浏览 55

在验证失败的情况下防止bootstrap模态消失

I'm trying to stop bootstrap modal from disappearing in case there is any invalid data entered by user.

my php validation code is:

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

  • 写回答

1条回答 默认 最新

  • dongxu1029 2015-12-23 05:25
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图