我正在尝试停止引导模式消失,以防用户输入任何无效数据。
我的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');
}
});
这显然不起作用。有什么建议吗?
答案 0 :(得分:0)
服务器端代码
<?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>
我希望这会有所帮助。