PHP不断获取单选按钮的最后一个元素

弗兰克·W。

我有以下问题:

我正在从单选按钮向ajax请求提交数据,以便可以将数据移至数据库。

-html

<form class="siteInfo" action="ajax/site.php?nr=<?php echo $_GET['nr']; ?>" method="POST">
<input type="radio" name="transport_transport_id" value="1"> <span class="value">1</span><br/>
<input type="radio" name="transport_transport_id" value="2"> <span class="value">2</span><br/>
<input type="radio" name="transport_transport_id" value="3" tabindex="21"> <span class="value">3</span><br/>

-ajax发布到的页面

foreach($_POST as $key => $val) {
    if(!empty($val)) {
        $result[$key] = $val;
        //This should be passed to database update function
    }
}

var_dump($_POST);

$site->setSiteFields($siteNumber, $result);

-阿贾克斯

$('.siteInfo').on('change', function() {
    var that = $(this);
    url = that.attr('action'),
    type = that.attr('method'),
    data = {};


    that.find('[name]').each(function(index, value){
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

        data[name] = value;
    });


    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response) {
            console.log(response);
        }
    });
    return false;
});

Ajax返回从拾取页面获得的响应,但是不管我选择哪个单选按钮,我只会得到返回的最后一个值。谁能告诉我哪里出了问题?

提前致谢!

hemnath mouli

试试这个

$('.siteInfo').on('change', function() {
    var that = $(this);
    url = that.attr('action'),
    type = that.attr('method'),
    data = {};


   data[name] = that.find("input[name='transport_transport_id']:checked").val();

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response) {
            console.log(response);
        }
    });
    return false;
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章