admin管理员组文章数量:1130349
I am creating a custom form in one of my paste and its getting submitted using AJAX post method. Here is my code:
HTML for button :
<button onclick="result()" type="button" name="result_submit" id="result_submit" >Submit</button>
Jquery:
function result(){
$.ajax({
url :ajaxurl,
type :'POST',
action :'expense_check',
success: function(data){
$("#result").html(data);
}
});
}
To use ajaxurl I added below php code in head section :
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
And then added below codes to my functions.php file:
add_action( 'wp_ajax_expense', 'expense_check' );
add_action( 'wp_ajax_nopriv_expense', 'expense_check' );
function expense_check(){
include_once 'dbConnection.php';
$stmt = mysqli_stmt_init($conn);
$income = "select SUM(amount) as incomeNumber FROM wp_formdata WHERE entry_type='Income'";
$response = '';
if (! mysqli_stmt_prepare($stmt,$income)) {
$response = '<h1 style="color:red;padding-top:5%;">SQL Error !!</h1>';
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$income_sum = mysqli_fetch_assoc($result);
$response = "Total Income is ".$income_sum['incomeNumber'];
}
echo $response;
}
But its not working. I am getting below error in console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost/wordpress/wp-admin/admin-ajax.php. (Reason: CORS request did not succeed).
How can I resolve this ?
I am creating a custom form in one of my paste and its getting submitted using AJAX post method. Here is my code:
HTML for button :
<button onclick="result()" type="button" name="result_submit" id="result_submit" >Submit</button>
Jquery:
function result(){
$.ajax({
url :ajaxurl,
type :'POST',
action :'expense_check',
success: function(data){
$("#result").html(data);
}
});
}
To use ajaxurl I added below php code in head section :
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
And then added below codes to my functions.php file:
add_action( 'wp_ajax_expense', 'expense_check' );
add_action( 'wp_ajax_nopriv_expense', 'expense_check' );
function expense_check(){
include_once 'dbConnection.php';
$stmt = mysqli_stmt_init($conn);
$income = "select SUM(amount) as incomeNumber FROM wp_formdata WHERE entry_type='Income'";
$response = '';
if (! mysqli_stmt_prepare($stmt,$income)) {
$response = '<h1 style="color:red;padding-top:5%;">SQL Error !!</h1>';
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$income_sum = mysqli_fetch_assoc($result);
$response = "Total Income is ".$income_sum['incomeNumber'];
}
echo $response;
}
But its not working. I am getting below error in console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost/wordpress/wp-admin/admin-ajax.php. (Reason: CORS request did not succeed).
How can I resolve this ?
本文标签: phpajaxurl usage for a custom function
版权声明:本文标题:php - ajaxurl usage for a custom function 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749240394a2337984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论