vendredi 6 mai 2016

how to get id of dynamiccaly created HTML hidden input in laravel 5?

I have an "Edit Form" where I am trying to update the values in the database as per the values in the form fields.

Below is the View(show.blade.php):

{!!Form::open(['route'=>'admin.poll.update'])!!}

<script type="text/javascript">
    $(document).ready(function(){  
    var c = 0;
    var template = "<div class=\"form-group add-polls-container\">"+'{!!Form::text("text","",["class"=>"form-control","placeholder"=>"Enter poll option","size"=>"15"])!!}'+'<a href="#" class="mi">Delete</a>'+"</div>";

    $('.pl').on('click',function(){
        /*e.preventDefault();*/
        var hiddenInput = document.createElement("input");
        var hiddenButton = document.createElement("button");
        var hiddenTextNode = document.createTextNode("Delete");

        $(this).after(hiddenInput);
        hiddenButton.appendChild(hiddenTextNode);
        $(this).after(hiddenButton);

        c=c+1; 
        document.getElementById("h").value = c;
        //hiddenInput.setAttribute("id", c);
        hiddenInput.name = 'text'+c;
        hiddenInput.class = 'form-control add-polls-container';
        hiddenInput.placeholder = 'Enter Poll Option';

        //hiddenTextNode.class = "mi";
        /*hiddenButton.text = "Delete";
        hiddenButton.value = "Delete";
        hiddenButton.style = "width:20px";*/
        //alert(hiddenInput.id);
    });

    $(document).on('click','.mi',function(){
        /*e.preventDefault();*/
        /*$(this).parent().remove();
        c=c-1;
        document.getElementById("h").value = c;*/
        alert('hi');
    });
});
</script>

        <?php
            $p_count = $p_count+1;
            echo "Poll Id:&nbsp".$p->id."&nbsp;Count:".$p_count;
        ?> 

        {!!Form::hidden('poll_id'.$p_count,$p->id
        )!!}

        {!!Form::hidden('count'.$p_count,$p_count)!!}

        {!!Form::label('title','Title:')!!}

        {!!Form::text('title'.$p->id,$p->title,[
        'class'=>'form-control',
        'placeholder'=>'Enter a title',
        'size'=>'15'

        ])!!} <br/>
        <?php //echo "Poll Id:&nbsp;".$p->id."<br/>";

        //for($i=0;$i<$c_options;$i++){
        //foreach($poll as  $value){
            ?>

            {!!Form::label('text','Poll Option:')!!}
            <?php
                $i = 0;
               foreach ($poll_option[$p->id] as $p_option ){
                $i = $i+1;
                  //echo "<pre>";print_r($p_option->text);
                  ?>
                  <div class="add-polls-container">
                  {!!Form::text('text'.$p->id.$i,$p_option->text,[
                    'class'=>'form-control',
                    'placeholder'=>'Enter poll option',
                    'size'=>'15'

                    ])!!}
                    </div>
                  <?php        



            }


        //}


       // }
            //echo $event->id;
        ?><div>
        {!!Form::hidden('event_id',$event->id,[
        'class'=>'form-control',
        'placeholder'=>'Enter event id',
        'size'=>'15',
        'style'=>'visibility:hidden'

        ])!!}



        {!!Form::submit('Update')!!}</div><div><a href="#" class="pl">Add more Options</a></div><br/>
        <!--<?php //} ?>-->

        <!--{!!Form::label('event_id','Event Id:',['style'=>'visibility:hidden'])!!}-->
        <input type="hidden" id="h" name="h" value=c>

        {!!Form::close()!!}

and the controller.php file is:

public function update(Request $request)
{
  //print_r($request->all());

    $poll = new Poll;
    $poll->id = $request->poll;
    print_r($request->poll_id);
    //$poll->text = $request->$count;
    //$poll->update();
    //return $poll->id."Updated"."<br/>";
    for($i=0; $i<=2;$i++)
     {
        $count = $request->count;
     }
    //return $request->count;
}

I want to get the id of hidden field and print it in the controller.php file. I am dynamically generating the id of hidden field.

Please give me solution.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/21CQ6mB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire