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 ) {
e.preventDefault();
var data = {
action: "star",
aid: $( this ).find( "input[name=aid]" ).val() // Get value of "aid" from current form submitting
};
$.post(
yes.ajaxurl,
data,
function( result ) {
window.alert( result );
$( "#myresult" ).html( result );
},
"json"
);
}
);