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
Add a comment  | 

1 Answer 1

Reset to default 0

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"
        );
    }
);

本文标签: phpPass the updated value of aid from form using ajax