samedi 9 mai 2015

Updated value of form item fetched with ajax

I am working in wordpress and I am using an ajax call to return aid field. I have a form in for loop and there are multiple submit buttons. I want the updated value of aidd field returned. aidd always return the first clicked value

jQuery(function ($) {
        $(document).on("click",".star", function(e) {
        e.preventDefault();

        var aid = jQuery("#aid").val();


        //////////////////////////////////
       alert(aidd);
             var sentdata =({

                action: 'star',
                    id:aid
            })
    var element = $(this);
     $.post(yes.ajaxurl, sentdata, function (res) { //start of funciton
    alert(id);
                $(element).siblings("#hero").html(res);
            } //end of function
            ,
            'json');    }); //end inner function
    });

php (just pasting the form php code )

$form.= '<form id="voteform" action="" method="post">';
    $form.='<div>';

    $form.= "<input id='aid' name='aid' type='text' value='$result->aid'>";
     ///////////////
    $form.="<input class='star' id='star1' type='image' type='submit' name='star5' src='http://localhost:8080/test/wp-content/uploads/2015/05/star0.png' value='1' style='border:0px!important;'>";
    $form.="<input class='star' id='star2' type='image' type='submit' name='star5' src='http://localhost:8080/test/wp-content/uploads/2015/05/star0.png' value='2' style='border:0px!important;'>";
    $form.="<input class='star' id='star3' type='image' type='submit' name='star5' src='http://localhost:8080/test/wp-content/uploads/2015/05/star0.png' value='3' style='border:0px!important;'>";
    $form.="<input class='star' id='star4' type='image' type='submit' name='star5' src='http://localhost:8080/test/wp-content/uploads/2015/05/star0.png' value='4' style='border:0px!important;'>";
    $form.="<input class='star' class='star' id='star5'  type='image' type='submit'  name='star5' src='http://localhost:8080/test/wp-content/uploads/2015/05/star0.png' value='5' style='border:0px!important;'>";

    $form.='<input class="star" name="star5" src="http://localhost:8080/test/wp-content/uploads/2015/05/star0.png" type="image" type="submit" value="6" /></form>';

Resulting php function

add_action( 'wp_ajax_star', 'star' );
add_action( 'wp_ajax_nopriv_star', 'star');


function star()
{


    $aid = $_POST['id'];
    echo json_encode ($aid);
    die();
}

Aucun commentaire:

Enregistrer un commentaire