Javascript 防止验证失败时引导模式消失

Javascript 防止验证失败时引导模式消失,javascript,php,jquery,twitter-bootstrap,Javascript,Php,Jquery,Twitter Bootstrap,我试图阻止引导模式消失,以防用户输入任何无效数据 我的php验证代码是: if (isset($_POST['submit'])) { $conn = new mysqli("localhost", "ahmesmat", "ZainMalek3110", "SignUpsIroners"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_er

我试图阻止引导模式消失,以防用户输入任何无效数据

我的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;        

}
if(isset($\u POST['submit'])){
$conn=newmysqli(“localhost”、“ahmesmat”、“ZainMalek3110”、“SignUpsIroners”);
如果($conn->connect\u错误){
die(“连接失败:”.$conn->connect\U错误);
}否则
如果(!$\u POST['lname'])
$error=“
您的名字。”; 如果(!$\u POST['fname']) $error.=“
您的姓。”; 如果(!$\u POST['email']) $error.=“
您的电子邮件地址。”; 如果(!(isset($发布日期)&&isset($发布月份)&&isset($发布年份)) $error.=“
您的完整出生日期。”; 如果(!$_POST['phone'])) $error.=“
您的电话号码。”; 如果(!$_POST['ssn'])) $error.=“
您的社会保险号码。”; 如果(!$\u POST['staddress']) $error.=“
您的街道地址。”; 如果(!$_POST['city']) $error.=“
您所在的城市。”; 如果(!$_POST['state'])) $error.=“
您的状态。”; 如果(!$\u POST['zcode']) $error.=“
请输入您的邮政编码。”; 如果(!$_POST['country']) $error.=“
您的国家/地区。”; 如果(!$_POST['radio'])) $error.=“
告诉我们您是否有熨斗或计划购买熨斗。”; } 如果(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');
    }
});

//这将在第一次启动modal
$(窗口)。加载(函数(){
$('myModal').modal('show');
});
//假设这是为了检索模态
$.ajax({
url:“signupstore.php”
}).done(函数(){
var标志=“”;
如果(标志==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>

客户端代码

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

//这将在第一次启动modal
$(文档).ready(函数(){
$('myModal').modal('show');
$.ajax({
url:'signupstore.php',
键入:“POST”,
数据:{email:'your_email',country:'your country'},//在此处传递数据
成功:功能(数据){
//成功时调用
如果(数据!=''){
$('.error').html(数据);//添加一个div以显示返回错误
}否则{
$('#myModal').modal('hide');
}
}
});    
});

我希望这有帮助。

对不起,如何使用data:{}传递数据?!以前从未做过。我用一个小例子更新了答案,你也可以参考:模式现在闪烁,这意味着我不能输入任何东西来测试重新显示。但是,对于传递的数据,我是否可以输入包含数据的变量,或者必须是静态文本?您可以使用jQuery示例检索数据:$('#inputId').val();此外,您还需要在“提交”单击时发送数据(用户默认设置)。由于模式仍在闪烁,因此不确定我是否以正确的方式发送数据<代码>//这将在第一次启动模式$(document).ready(function(){$(''#myModal').modal('show');event.preventDefault();$.ajax({url:'signupstore.php',type:'POST',var data=$('.#form#val').val();success:function(data){//如果(data!='')成功,则调用函数(data){$('.error').html(data);//添加一个div以显示返回错误}else{$('#myModal').modal('hide');}}}}}});