1
我想使用甜蜜警報JS顯示在Ajax表單提交警報。通過做一些研究,我已經想出瞭如何使用ajax發佈,但無法使用甜蜜警報正常運行以顯示警報。
ajax的甜蜜警報 - 無法找出帖子後的回覆消息
的index.html:
<!DOCTYPE html>
<html>
<head>
<link data-require="[email protected]*" data-semver="0.4.2" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/0.4.2/sweet-alert.min.css" />
<script data-require="[email protected]*" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script data-require="[email protected]*" data-semver="0.4.2" src="//cdnjs.cloudflare.com/ajax/libs/sweetalert/0.4.2/sweet-alert.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<form class="form-horizontal" name="addSaleForm" id="addSaleForm" method="POST">
<label>First Name:</label><br>
<input type="text" name="fname" id="fname"><br>
<button type="submit" id="submit-btn" name="register">Submit</button>
</form>
<!-- Display result/error msg from php file -->
<div id="status"></div>
</body>
</html>
<script>
$(document).ready(function(e) {
$("#addSaleForm").on('submit', (function(e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "Do you want to submit it?",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes!',
cancelButtonText: "No!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal(
function() {
$.ajax({
url: "process.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(data) {
if (data === 'Success') {
swal("Processed!!!", "Your form is processed successfully", "success");
}
else {
document.getElementById("status").className += " alert-danger";
$("#status").html(data);
swal("Error!!!", data, "error");
}
},
error: function() {}
});
});
} else {
swal("Cancelled", "Your form is cancelled.", "error");
}
});
}));
});
</script>
process.php
<?php
if (isset($_POST['fname'])) {
echo "Success";
}
else{
echo "Please enter your first name.";
}
?>
而這裏的上述代碼的真人版。
http://plnkr.co/edit/maZs1NjFlxzqoFpGLgoe?p=preview
您的代碼似乎是做工精細,但你process.php拒絕形式 - 它需要一個「文件」字段有(「信息」:「文件要求」)。 –
@denis:什麼樣的文件? – sohal07