Welcome Guest, Not a member yet? Register   Sign In
Pass multidimensional array created with Jquery through hidden field to Codeigniter
#1

[eluser]elambert[/eluser]
I have a form where the user can add items into inventory. Here is a screenshot: http://www.internetgreenhouse.com/add-directv.html

They build a list of Equipment Types and Quantities they want to add which is done using Jquery. The problem I am having is creating a multidimensional array with this information e.g. { equipment_type_id: '10', equipment_type_name: 'WDECA - WIRELESS DECA', equipment_quantity: '3' } and passing it to Codeigniter via a hidden form.

What is the best way to accomplish this?
#2

[eluser]Eduard Stankovic[/eluser]
check this jquert methd $.post()

http://api.jquery.com/jQuery.post/

#3

[eluser]elambert[/eluser]
Ed, thank you for your reply.

Actually, I do not want to submit the form via ajax. I am just adding adding items dynamically with Jquery into hidden fields and submitting the form with a standard post. I have a solution developed (see below), I just do not feel that it is very elegant. Currently, I have an array for each field I want save. Ideally, I would add all the fields into a single multidimensional array. Here is how I am doing it right now.

Jquery
Code:
$("#add_button").click(function() {
  non_serialized_cnt++;
  non_serialized_equipment_type_id = $('#non_serialized_equipment_types_equipment_type_id').val();
  non_serialized_equipment_type_value = $("#non_serialized_equipment_types_equipment_type_id :selected").text();
  non_serialized_quantity = $('#item_quantity').val();    

  str = "<tr id='row_" + non_serialized_cnt + "'>";
  str += "<td class='first'><div>&nbsp;</div></td>";  
  str += "<td class='equipment_type'>" + non_serialized_equipment_type_value + "</td>";
  str += "&lt;input type='hidden' id='non_serialized_equipment_types_selected[]' name='non_serialized_equipment_types_selected[]' value='" + non_serialized_equipment_type_id + "' /&gt;";
  str += "&lt;input type='hidden' id='non_serialized_equipment_types_selected_text[]' name='non_serialized_equipment_types_selected_text[]' value='" + non_serialized_equipment_type_value + "' /&gt;";
  str += "&lt;input type='hidden' id='non_serialized_equipment_quantities[]' name='non_serialized_equipment_quantities[]' value='" + non_serialized_quantity + "' /&gt;&lt;/td>";  
  str += "<td class='non_serialized_quantity'>" + non_serialized_quantity + "</td>";  
  str += "<td class='actions'><a href='#' class='delete_non_serialized'>delete</a></td>";    
  str += "</tr>";
      
  $(str).prependTo(".non_serialized > table > tbody");

  $('#item_quantity').val('');
  $("#non_serialized_equipment_types_equipment_type_id").val('');
        
  $("a.delete_non_serialized").unbind();
  $("a.delete_non_serialized").bind('click', function() {    
   $(this).closest("tr").remove();
   return false;  
  });  
});

View (this repopulates the arrays into the hidden fields when form validation fails)
Code:
&lt;? if ( isset( $non_serialized_equipment_types_selected ) && is_array( $non_serialized_equipment_types_selected ) && count( $non_serialized_equipment_types_selected ) > 0 ): ?&gt;
    &lt;? foreach( $non_serialized_equipment_types_selected as $index => $non_serialized_selected ):?&gt;
     &lt;input type="hidden" name="non_serialized_equipment_types_selected[]" id="non_serialized_equipment_types_selected[]" value="&lt;?= $non_serialized_selected; ?&gt;"&gt;
     &lt;input type="hidden" name="non_serialized_equipment_types_selected_text[]" id="non_serialized_equipment_types_selected_text[]" value="&lt;?= ( isset( $non_serialized_equipment_types_selected_text ) ? $non_serialized_equipment_types_selected_text[ $index ] : set_value( 'non_serialized_equipment_types_selected_text[ $index ]' ) ); ?&gt;"&gt;
     &lt;input type="hidden" name="non_serialized_equipment_quantities[]" id="non_serialized_equipment_quantities[]" value="&lt;?= ( isset( $non_serialized_equipment_quantities ) ? $non_serialized_equipment_quantities[ $index ] : set_value( 'non_serialized_equipment_quantities[ $index ]' ) ); ?&gt;"&gt;
    &lt;? endforeach ?&gt;
   &lt;? endif ?&gt;

In the Controller and Model I just access it the same way I do all the form data...

I am sure that others have solved this problem before...Any ideas on how to improve on my current solution?




Theme © iAndrew 2016 - Forum software by © MyBB