我正在尝试为一个项目设置模态,在这个项目中,客户可以更新自己喜欢的球队,出生城市和他们的体型。在没有输入一些信息时,我希望将错误消息返回给他们,但是回显的唯一错误信息是“由于没有提交新信息,城市未更新。”
模态代码如下:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria- labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="city">You were born in:</label>
<input type="text" class="form-control" id="city" placeholder="city">
</div>
<div class="form-group">
<label for="favorite_team">Favorite team:</label>
<input type="text" class="form-control" id="favoriteTeam" placeholder="favorite team">
</div>
<div class="form-group">
<label for="size">size:</label>
<input type="text" class="form-control" id="size" placeholder="size">
</div>
<div class="alert alert-success" id="successAlert" role="alert" style="display: none"></div>
<div class="alert alert-danger" id="updateFail" style="display:none" > </div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" >Close</button>
<button type="button" class="btn btn-primary" id="saveBtn" onClick="Changes()">Save changes</button>
</div>
</div>
</div>
</div>
我的ajax代码是:
function Changes(){
$.ajax({
type: "POST",
url: "../php_parsers/update_parse.php",
data: "city=" + $("#city").val() + "&favoriteTeam=" + $("#favoriteTeam").val() + "&size=" + $("#size").val(),
success: function(result) {
if (result == "success") {
$("#successAlert").html(result).show();
} else {
$("#updateFail").html(result).show();
}
}
})
}
我的php代码是update_parse.php:
<?php
// AJAX CALLS THIS UPDATE CODE TO EXECUTE
$error = "";
$success = "";
if(!$_POST['city']){
$error .= "City was not updated as there was not new information submitted.";
} else if(!$_POST['team']){
$error .= "Team was not updated as there was not new information submitted.";
} else if(!$_POST['size']){
$error.= "size was not updated as there was not new information submitted.";
}
if($error == ""){
echo $error;
}else {
$success = "update successful so far";
echo $success;
}
?>
欢迎所有反馈。
答案 0 :(得分:2)
您正在使用if else if
构造进行检查,最多只会报告一个错误。将其更改为独立的if语句
if(!$_POST['city']){
$error .= "City was not updated as there was not new information submitted.";
}
if(!$_POST['team']){
$error .= "Team was not updated as there was not new information submitted.";
}
if(!$_POST['size']){
$error.= "size was not updated as there was not new information submitted.";
}
答案 1 :(得分:1)
尝试像这样建立你的AJAX
$.ajax({
url: "../php_parsers/update_parse.php",
type: "POST",
data: {
city: $("#city").val(),
favoriteTeam: $("#favoriteTeam").val(),
size: $("#size").val()
}
}).done(function(result) {
if (result == "success") {
$("#successAlert").html(result).show();
} else {
$("#updateFail").html(result).show();
}
})
也不是你的回归有点时髦
if($error != ""){ <------------- Change
echo $error;
}else {
$success = "update successful so far";
echo $success;
}
答案 2 :(得分:1)
尝试将您的参数设置为此
var param= {
"city" : $("#city").val(),
"favoriteTeam" : $("#favoriteTeam").val(),
"size" : $("#size").val()
};
然后在你的ajax属性data
中使它像这样:
$.ajax({
type: "POST",
url: "../php_parsers/update_parse.php",
data: param,
success: function(result) {
if (result == "success") {
$("#successAlert").html(result).show();
} else {
$("#updateFail").html(result).show();
}
}
})