$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - ajaxurl usage for a custom function|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - ajaxurl usage for a custom function

matteradmin10PV0评论

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 ?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far