admin管理员组文章数量:1122832
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";
jQuery
$(document).on("click",".star", function(e) {
e.preventDefault();
var aidd = jQuery("#aid").val();
sentdata =({
action: 'star',
aid:aidd,
})
$.post(yes.ajaxurl, sentdata, function (res) { //start of funciton
alert(aid);
$("#myresult").html(res);
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);
die();
}
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";
jQuery
$(document).on("click",".star", function(e) {
e.preventDefault();
var aidd = jQuery("#aid").val();
sentdata =({
action: 'star',
aid:aidd,
})
$.post(yes.ajaxurl, sentdata, function (res) { //start of funciton
alert(aid);
$("#myresult").html(res);
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);
die();
}
Share
Improve this question
asked May 9, 2015 at 10:02
ZeeshanZeeshan
1712 gold badges5 silver badges19 bronze badges
1 Answer
Reset to default 0I'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"
);
}
);
本文标签: phpPass the updated value of aid from form using ajax
版权声明:本文标题:php - Pass the updated value of aid from form using ajax 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281148a1926227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论