0

Where is my mistake? html code

<div class="star-rating">
<span class="postid" post-id="52"></span>
<span class="score-bg"></span>
<div class="scores">
    <?php for ($i = 1; $i <= 5; $i++): ?>
        <div class="score" data-score="<?php echo $i; ?>">
            <span class="scoreAbsolute"></span>
        </div>
    <?php endfor; ?>
</div>
jQuery(document).ready(function ($) {
$(function () {
    $(".score").click(function () {
        var score = $(this).attr('data-score');
        var post = $('.postid').attr('post-id');
        $.ajax({
            url: post_score.ajaxscript,
            method: 'POST',
            data: {
                'action': 'submit_score',
                'postid': post,
                'postScore': score,
            },
            success: function (data) {
                console.log(data);
            }, error: function (errorThrown) {
                alert('nok');
            }
        });
    });
});

});

php code

    add_action('wp_ajax_submit_score', 'submit_score');
    add_action('wp_ajax_nopriv_submit_score', 'submit_score');
    
    function submit_score()
    {
        if (isset($_POST['action'])) {
    //something
        }
        wp_die();
    }
5
  • Where is your PHP code? Dec 2, 2022 at 17:02
  • @JacobPeattie last add_action('wp_ajax_submit_score', 'submit_score'); add_action('wp_ajax_nopriv_submit_score', 'submit_score'); function submit_score() { if (isset($_POST['action'])) { //something } wp_die(); }
    – Tara
    Dec 2, 2022 at 20:44
  • I asked where is the code? Dec 3, 2022 at 3:32
  • @JacobPeattie in my plugin
    – Tara
    Dec 3, 2022 at 3:56
  • your submit_score function isn't doing anything, what are you expecting it to do? And can you add some context for what it's meant to do? What are you trying to implement and where are the code snippets you shared located? Use the Edit link under the tags to update your question so that it's easier to understand what you're asking for
    – Tom J Nowell
    Dec 3, 2022 at 17:48

2 Answers 2

0

Does you have localized your script correctly? Please try the following method to enqueue your custom script and localize it.

add_action( 'wp_enqueue_scripts', 'my_custom_scripts', 10 );
function my_custom_scripts() {
    wp_enqueue_script('custom-script',plugin_dir_url(__FILE__) . '/custom_script.js',
        array( 'jquery' )
    );
    wp_localize_script( 'custom-script', 'ajaxscript',array( 'ajaxurl' => admin_url('admin-ajax.php' )));
}

Then you can call ajax url in custom_script.js as this way.

        $.ajax({
            url: ajaxscript.ajaxurl,
            method: 'POST',
            ....
        });
4
  • Yes I have. And it works well.
    – Tara
    Dec 3, 2022 at 12:36
  • @Tara does this answer your question? If it solves your question consider marking it as the correct answer by clicking the tick button under the voting controls
    – Tom J Nowell
    Dec 3, 2022 at 17:49
  • @TomJNowell no!
    – Tara
    Dec 3, 2022 at 17:55
  • 1
    that's unfortunate, but can you add some details then so that irfan can make improvements to their answer? Other users might also think that Irfan solved your problem and move on to other questions if you ignore it
    – Tom J Nowell
    Dec 4, 2022 at 0:52
0

your onclick class is not in the HTML. you are calling $(".post-score").click when in your HTML file the class name is .score

in your PHP code you want to handle the REQUEST. you dont have to check if the action is posted because wordpress is handling that for you by calling the correct function.

so your JS should be defined like this:

data: {
    'action': 'submit_score',
    postid: post,
    ticketScore: score,
},

and in your PHP:

add_action('wp_ajax_submit_score', 'submit_score');
add_action('wp_ajax_nopriv_submit_score', 'submit_score');
    
function submit_score(){
    $safe_postID = intval( $_REQUEST['postid'] );
    if ( ! $safe_postID ) {
        $safe_postID = '';
    }
    
    $safe_ticketScore = intval( $_REQUEST['ticketScore'] );
    if ( ! $safe_ticketScore ) {
        $safe_ticketScore = '';
    }
    
    if($safe_postID && $safe_ticketScore ){
        // RUN YOUR CODE AND OUTPUT YOUR SUCCESS
        wp_send_json_success( array( 'postID' => $safe_postID, 'ticketScore' => $safe_ticketScore ) );
    }
    
    wp_send_json_error();
    wp_die();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.