dongmanzui8486 2012-05-08 19:25
浏览 123
已采纳

HTML中的多个隐藏字段

I am creating a popup dialog box where I need to put a set of values in hidden format, but when I am getting the value in AJAX post, I am getting only last value.

this is the PHP part:

$plan_ids=array();
foreach($test_plan as $plan)
{
    $plan_ids[]=$plan['plan_id'];
}
?>

<?php
foreach($plan_ids as $id)
{
    echo "<input type='hidden' id='plan_id' value='$id'>";
}
//var_dump($plan_ids);
// echo $plan['plan_id'];
?>

In the AJAX part I am doing:

$("#save").click(function () {
    var name = $('#name').val();
    var id = $('#release_id').val();
    var plan_id = $('#plan_id').val();
    //alert('hello');
    $.ajax({
        type: 'POST',
        url: '/api/api.php?action=put_iteration&name=' + name + '&id=' + id + '&plan_id=' + plan_id,
        data: "name=" + name + "&id=" + id + "&plan_id=" + plan_id,
        success: function () {
            $('#save').hide(function () {
                $('div.success').fadeIn();
            });
        }
    });
});

I'm clueless about HTML hidden fields.

  • 写回答

4条回答 默认 最新

  • dongsechuan0535 2012-05-08 19:35
    关注

    you can name all your hidden fields like an array name="plan_id[]"

    And instead of passing it as a string you can have a wrapping FORM around hidden fields and then use jquery serialize function to POST it

    Now you will get all the plan_id in the form of an array in the POST variable

    Adding an example

    <?php
    echo '<form name="planidform" id="planidform">';
    foreach($plan_ids as $id)
    {
        echo "<input type='hidden' name="plan_id[]" value='$id'>";
    }
    echo '</form>';
    ?>
    

    After than in jQuery do it in following manner:

    data: "name=" + name + "&id=" + id + "&"+$("#planidform").serialize(),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大