learning_bee
learning_bee

Reputation: 165

Multiple hidden fields in 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.

Upvotes: 0

Views: 2147

Answers (4)

swapnilsarwe
swapnilsarwe

Reputation: 1300

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(),

Upvotes: 1

galtzhayek
galtzhayek

Reputation: 75

you should put diffrent names / ids to the hidden fields. if u want to submit them all at once u can store them in an array. for example:

$i=0;
foreach($plan_ids as $id){
$i++;
echo "<input type='hidden' id='plan_id_$i' value='$id'>";}

then u can address or group them in JS.

Upvotes: 1

Shyju
Shyju

Reputation: 218792

Not a PHP guy, But some thoughts . Forgive me for syntax errors.

In the loop You are creating hidden element with the same ID. That is not good. Change the code sot hat the Id's will be ( SHOULD BE ALWAYS ) Unique.

<div>
foreach($plan_ids as $id)
{
    echo "<input type='hidden' id='plan-$id' value='$id' class='myHidden'>";
}

Now in your script, use jQuery selectors based on the hidden item

var hiddenItems=$("input[type='hidden']");

may be now you can loop thru this

   var items
   $.each(hiddenItems,function(item,index){
      items+= hiddenItems[index];
   });

Or you can map function like this so that it will give a list of values of hidden fields comma seperated.

   var itemsJoined=$("input[type='hidden']").map(function () {
        return this.value;
    }).get().join(',');

Upvotes: 2

vicatcu
vicatcu

Reputation: 5857

I think you want to change id='plan_id' to name='plan_id[]' for a start.... you are only allowed to have one element with a given id (i.e. id is required to be unique across elements in a given page).

Upvotes: 1

Related Questions