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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)