忍耐與我我是我的JavaScript有點生疏。所以我試圖使用ajax調用一個PHP文件,並給它一個計劃類型,然後檢查它是否檢查,看它是否返回true或false,如果某些允許的插槽少於某些插槽用完計劃。這是XHTML中的表單。Ajax無法正常工作
<form method="post" action="/membership-change-success" id="PaymentForm">
<input type="hidden" name="planChosen" id="planChosen" value="" />
</form>
在同一個文件上。 (< PLAN CHOICE>)被解析爲當前的計劃。
<script>
var hash = window.location.hash;
var currentPlan = "(< PLAN CHOICE >)";
$(".planChoice").click(function(event){
var isGood=confirm('Are you sure you want to change your plan?');
var success;
$("#planChosen").val($(this).data("plan"));
$.ajax({
url: '/ajax/planCheck.php',
type: "POST",
dataType: 'json',
data: ({plan: $(this).data("plan")}),
success: function (data) { //This is what is not working I can't get it to return true
success = data;
}
});
if(success) {
if (isGood) {
$("#PaymentForm").submit();
}
window.location = '/membership-change-success';
} else {
alert('Please make sure you deactivate your listings to the appropriate amount before you Downgrade.')
}
});
我的PHP的Ajax響應看起來像這樣。
<?php
require ('../includes/common.php');
include_once ('../includes/db-common.php');
require ('../includes/config.php');
$membership = new membership($dbobject);
$listing = new listing($dbobject);
$totalAvailableListings = ($membership->get_listingsAmount($_POST['plan']));
if($totalAvailableListings>=$listing->get_active_listings($user->id)){
echo json_encode(true); // I've tried with out jason_encode too
} else {
echo json_encode(false);
}
而這就是如果你有任何建議,請讓我知道。
所以我試圖以另一種方式做到這一點。
$(".planChoice").click(function (event) {
var isGood = confirm('Are you sure you want to change your plan?');
var success;
$("#planChosen").val($(this).data("plan"));
if (false) {
if (isGood) {
$("#PaymentForm").submit();
alert('you did it');
}
} else {
alert(isSuccessful($(this).data("plan")));
//alert('Please make sure you deactivate your listings to the appropriate amount before you downgrade.');
}
});
,我有一個Ajax功能
function isSuccessful(plan) {
return $.ajax({
url: '/ajax/planCheck.php',
type: "POST",
dataType: 'json',
data: {plan: plan}
});
}
警報告訴我這[XmlHttpRequest對象]
有什麼建議?
那麼你的問題是什麼? – epascarello
'成功:成功=數據'是錯誤的你應該有一個回調執行 – epascarello
https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello