I am working in wordpress and I want to fetch the updated value of aid field from form each time a submit button is pressed. There are two submit buttons and I want the id as per the clicked row

HTML Form(it is shown dynamically with php code)

foreach( $results as $result ) {
$form.= '<form id="voteform" action="" method="post">';
$form.= "<input id='aid' name='aid' type='text' value='$result->aid'>";

$form.=" <input class='star' class='star' id='star5'  type='submit'  name='star5' value='5'>";
$form.=" <input class='star' class='star' id='star6'  type='submit'  name='star5' value='5'></form";


$(document).on("click",".star", function(e) {
var aidd = jQuery("#aid").val();
sentdata =({

            action: 'star',

$.post(yes.ajaxurl, sentdata, function (res) { //start of funciton

            return false;
        } //end of function
        'json');    }); //end inner function
}); //end main function

php code

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

function star()

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

1 Answer 1


I'm a bit confused - why the two submit buttons? And you're outputting the same id multiple times (inside a foreach loop), which will choke jQuery. Try the following, using classes:

$form = '';
foreach ( $results as $result ) {
    $value = esc_attr( $result->aid );
    $form .= '<form method="post" class="aid-form">';
        $form .= "<input name='aid' type='text' value='$value' />";
        $form .= "<input name='star5' class='star' type='submit' value='5' />";
    $form .= '</form>';

And then your jQuery:

$( document ).on( "submit", ".aid-form",
    function( e ) {
        var data = {
            action: "star",
            aid: $( this ).find( "input[name=aid]" ).val() // Get value of "aid" from current form submitting       

            function( result ) {
                window.alert( result );
                $( "#myresult" ).html( result );

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.