我尝试,当单击submit按钮时,用一些值传递ajax请求,然后计算这些值并返回。
My Problem : 当我进行自定义Ajax调用时,我从管理Ajax得到400个错误。php。请帮我找出我的错误或最好的方法来做这件事。
Ajax Call - footer.php(theme footer)
<script>
$(function() {
$("#calc_form").on("submit", function(e) {
// alert("submit");
e.preventDefault();
$.ajax({
method: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: \'calculation\',
message_id: $(\'#tot_tax_three_month_Input\').val()
},
dataType: \'json\',
success: function (output) {
}
});
});
});
</script>
Admin-ajax.php
// handle the ajax request
function calculation() {
$value = $_REQUEST[\'message_id\'];
echo json_encode(array("value" => $value));
die();
}
// register the ajax action for authenticated users
add_action(\'wp_ajax_calculation\', \'calculation\');
// register the ajax action for unauthenticated users
add_action(\'wp_ajax_nopriv_calculation\', \'calculation\');
test.php
<form id="calc_form">
<table class="table">
<thead>
<tr>
<th style="width: 40%" scope="col"></th>
<th scope="col">Per month LKR</th>
<th scope="col">For 3 months period* LKR</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2" class=" text-danger"><b>Tax amount for 3 months</b></td>
<td><input type="text" name="" id="tot_tax_three_month_Input" class="form-control " /></td>
</tr>
<tr>
<td colspan="2"></td>
<td>
<button type="submit" id="btnCalculate" class="btn btn-success">Calculate</button>
<button type="button" id="btnReset" class="btn btn-secondary">Reset</button>
</td>
</tr>
</tbody>
</table>
</form>