Post the id on delete in codeigniter












0















I have two tables users and posts where is the primary key is id. When i click on delete button, i want to post the id.



Here is my view:



<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>









share|improve this question

























  • HTML side note: <form> cannot be made a child of <table>.

    – Funk Forty Niner
    Jan 2 at 5:33
















0















I have two tables users and posts where is the primary key is id. When i click on delete button, i want to post the id.



Here is my view:



<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>









share|improve this question

























  • HTML side note: <form> cannot be made a child of <table>.

    – Funk Forty Niner
    Jan 2 at 5:33














0












0








0








I have two tables users and posts where is the primary key is id. When i click on delete button, i want to post the id.



Here is my view:



<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>









share|improve this question
















I have two tables users and posts where is the primary key is id. When i click on delete button, i want to post the id.



Here is my view:



<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>






php forms codeigniter session codeigniter-3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 5:34









Funk Forty Niner

1




1










asked Jan 1 at 13:49









absalam48absalam48

12




12













  • HTML side note: <form> cannot be made a child of <table>.

    – Funk Forty Niner
    Jan 2 at 5:33



















  • HTML side note: <form> cannot be made a child of <table>.

    – Funk Forty Niner
    Jan 2 at 5:33

















HTML side note: <form> cannot be made a child of <table>.

– Funk Forty Niner
Jan 2 at 5:33





HTML side note: <form> cannot be made a child of <table>.

– Funk Forty Niner
Jan 2 at 5:33












3 Answers
3






active

oldest

votes


















0














There are so many ways to do this, the best most common one is to get it from the url like this:



$id = @end($this->uri->segment_array());


Or this:



$id = $this->uri->segment(3);


Or you can do this by passing it in a hidden input like this:



<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>


Or using ajax.






share|improve this answer































    0














    delete link with confimation



    <a href="javascript:;" class="btn btn-danger" onclick="delete('<?php echo $post['id'] ?> ')">Delete</a>


    call delete method in js and then show confimation msg.






    share|improve this answer































      0














      Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly



      <td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>


      In controller delete function will be like this



      public function delete($id){
      echo $id;
      }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995985%2fpost-the-id-on-delete-in-codeigniter%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        There are so many ways to do this, the best most common one is to get it from the url like this:



        $id = @end($this->uri->segment_array());


        Or this:



        $id = $this->uri->segment(3);


        Or you can do this by passing it in a hidden input like this:



        <?php echo form_open('/posts/delete/'.$post['id']); ?>
        <input type="submit" value="Delete" class="btn btn-danger">
        <input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
        </form>


        Or using ajax.






        share|improve this answer




























          0














          There are so many ways to do this, the best most common one is to get it from the url like this:



          $id = @end($this->uri->segment_array());


          Or this:



          $id = $this->uri->segment(3);


          Or you can do this by passing it in a hidden input like this:



          <?php echo form_open('/posts/delete/'.$post['id']); ?>
          <input type="submit" value="Delete" class="btn btn-danger">
          <input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
          </form>


          Or using ajax.






          share|improve this answer


























            0












            0








            0







            There are so many ways to do this, the best most common one is to get it from the url like this:



            $id = @end($this->uri->segment_array());


            Or this:



            $id = $this->uri->segment(3);


            Or you can do this by passing it in a hidden input like this:



            <?php echo form_open('/posts/delete/'.$post['id']); ?>
            <input type="submit" value="Delete" class="btn btn-danger">
            <input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
            </form>


            Or using ajax.






            share|improve this answer













            There are so many ways to do this, the best most common one is to get it from the url like this:



            $id = @end($this->uri->segment_array());


            Or this:



            $id = $this->uri->segment(3);


            Or you can do this by passing it in a hidden input like this:



            <?php echo form_open('/posts/delete/'.$post['id']); ?>
            <input type="submit" value="Delete" class="btn btn-danger">
            <input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
            </form>


            Or using ajax.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 3:04









            Sherif SalahSherif Salah

            1,0941516




            1,0941516

























                0














                delete link with confimation



                <a href="javascript:;" class="btn btn-danger" onclick="delete('<?php echo $post['id'] ?> ')">Delete</a>


                call delete method in js and then show confimation msg.






                share|improve this answer




























                  0














                  delete link with confimation



                  <a href="javascript:;" class="btn btn-danger" onclick="delete('<?php echo $post['id'] ?> ')">Delete</a>


                  call delete method in js and then show confimation msg.






                  share|improve this answer


























                    0












                    0








                    0







                    delete link with confimation



                    <a href="javascript:;" class="btn btn-danger" onclick="delete('<?php echo $post['id'] ?> ')">Delete</a>


                    call delete method in js and then show confimation msg.






                    share|improve this answer













                    delete link with confimation



                    <a href="javascript:;" class="btn btn-danger" onclick="delete('<?php echo $post['id'] ?> ')">Delete</a>


                    call delete method in js and then show confimation msg.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 5:02









                    PHP GeekPHP Geek

                    1,3861718




                    1,3861718























                        0














                        Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly



                        <td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>


                        In controller delete function will be like this



                        public function delete($id){
                        echo $id;
                        }





                        share|improve this answer




























                          0














                          Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly



                          <td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>


                          In controller delete function will be like this



                          public function delete($id){
                          echo $id;
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly



                            <td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>


                            In controller delete function will be like this



                            public function delete($id){
                            echo $id;
                            }





                            share|improve this answer













                            Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly



                            <td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>


                            In controller delete function will be like this



                            public function delete($id){
                            echo $id;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 5:29









                            Danish AliDanish Ali

                            1,5002919




                            1,5002919






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995985%2fpost-the-id-on-delete-in-codeigniter%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas