Search code examples
phphtmllaravellaravel-3

Passing a portlet id to a popup modal after pressing a button - Laravel


I have some portlets here that has an ID for each of it. What I'm trying to do is to pass these IDs to a popup modal after pressing an Add button in the portlet.

Here is the portlet view:

@foreach($portlets as $portlet)
<div class="box span4">
  <div class="box-header well" data-original-title>
    <h2><i class="icon-th"></i> {{$portlet->portlet_title}} </h2>
  </div>
  <div class="box-content">
    <div class="row-fluid">
      // some contents over here
    </div>
    <div class="box-icon">
      <a href="#" class="btn btn-settingLink btn-round" title="Add New Link" data-rel="tooltip"><i class="icon-plus-sign"></i></a>
    </div>
  </div>
</div>
@endforeach

Notice the Add New Link button at the bottom of the code, when I press that, a modal will appear. Here is the view of it:

<div class="modal hide fade" id="linkSettings">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3>Add New Link</h3>
  </div>

  <form method="post" action="{{ URL::to('addlink') }}" >
  <div class="modal-body">
      <div class="control-group">
      <label class="control-label" for="focusedInput">Link Title:</label>
      <div class="controls">
        <input class="input-xlarge focused" id="link_title" name="link_title" type="text">
      </div>
      </div>

      <div class="control-group">
      <label class="control-label" for="focusedInput">Add A Link:</label>
      <div class="controls">
        <input class="input-xlarge focused" id="link_url" name="link_url" placeholder="eg. http://www.example.com/" type="text">
      </div>
      </div>
  </div>

  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <input type="submit" id="submit" name="submit" value="Submit" class="btn btn-primary">
  </div>
  </form>
</div>

This modal has a form in it to be processed in the controller. But how do I send the portlet ID to this modal so that it can be stored in the database during processing in the controller? Because I want it to automatically know what portlet I want to add links in it. Any idea?

If anything, please let me know.


Solution

  • You could attach a jQuery event handler to clicking the "Add New Link" button, and use it to populate a hidden field in the modal view.

    i.e. something like:

    $("a.new_link").click(function(){
        portlet_id = $(this).parent().parent().attr("id");
        $("div.modal form input.my_hidden_field").val(portlet_id)
    });
    

    And somewhere in the <form> for the modal:

    <input type="hidden" name="portlet_id" class="my_hidden_field"/> 
    

    Then, you just need to get the portlet_id POST parameter with Laravel/PHP to determine the portlet ID that was used.

    Edit

    Revised handler based on the code you gave:

    $('.btn-settingLink').click(function(e){
        e.preventDefault();
        $('#linkSettings').m‌​odal('show');
        // get the portlet_id and modify the midden field
        portlet_id = $(this).parent().parent().attr("id");
        $("div.modal form input.my_hidden_field").val(portlet_id)
    });