创建一个函数来编辑和删除 table - Laravel 5.2 中的输入

Creating a function to edit and delete inputs in a table - Laravel 5.2

我有一个代码是这样工作的:

表单页面(phonebook.blade.php):

....... some css here and stuff .......

<body>

        <h1>Contact Form</h1><br/>

                    {{-- FORM --}}

        <form method = "POST" action = "contacts">

            {{ csrf_field() }}

            <div class = "form-group"> 
                <label for = "name">Name</label><br/>
                <input type = "text" id = "name" class = "form-control" name = "name" placeholder = "Name your Name" value = "{!! old('name') !!}">
            </div><br/>

            <div class = "form-group"> 
                <label for = "lastname">Lastname</label><br/>
                <input type = "text" id = "lastname" class = "form-control" name = "lastname" placeholder = "Name your Lastname" value = "{!! old('lastname') !!}">                        
            </div><br/>

            <div class = "form-group 
                @if($errors->has('email')) 
                    has-error
                @endif">
                <label for = "email">E-mail</label><br/>
                <input type = "text" id = "email" class = "form-control" name = "email" placeholder = "somesomething@email.com" >
                @if($errors->has('email')) 
                    <p class = "help-block">{{ $errors->first('email') }}</p> 
                @endif
            </div><br/>

            <div class = "form-group 
                @if($errors->has('phone'))
                    has-error
                @endif">
                <label for = "phone">Phone Number</label><br/>
                <input type = "text" id = "phone" class = "form-control" name = "phone" placeholder = "I'll call you">
                @if($errors->has('phone')) 
                    <p class="help-block">{{ $errors->first('phone') }}</p> 
                @endif
            </div><br/>

            <div class = "form-group"> 
                <label for = "address">Address</label><br/>
                <input type = "text" id = "address" class = "form-control" name = "address" placeholder = "I'll even visit you" value = "{!! old('address') !!}">                        
            </div><br/>

            <button type = "submit" class = "submit">Submit Information</button>
            <a href="contacts"><button type = "button">View Contacts</button></a>
        </form>      
    </body>
</html>

当我点击“Submit Information”按钮时,联系人存储在 'contacts' table 中,我被重定向到页面“contacts.blade.php”。

当我点击“View contacts”按钮时,我也被重定向到“contacts.blade.php”。

这是“contacts.blade.php

....... some css and scripts here .......


<body>
        <h1>Contacts</h1>

        <br/>

        <a href = "phonebook"><button class = "ret" type = "button">Add New Entry</button></a>

        <br/><br/>

        <table class = "contacts">

            <thead>
                <tr>
                    <th> ID       </th>
                    <th> Name     </th>
                    <th> Lastname </th>
                    <th> E-Mail   </th>
                    <th> Phone    </th>
                    <th> Address  </th>
                    <th> Edit     </th>
                    <th> Delete   </th>
                </tr>
            </thead>

            <tbody>
                @foreach($contact as $contact)                                                      
                    <tr class = "tableBody">
                        <td class = "id">       {{ $contact->id }}                                                      </td>
                        <td class = "name">     {{ $contact->name }}                                                    </td>
                        <td class = "lastname"> {{ $contact->lastname }}                                                </td>
                        <td class = "email">    {{ $contact->email }}                                                   </td>
                        <td class = "phone">    {{ $contact->phone }}                                                   </td>
                        <td class = "address">  {{ $contact->address }}                                                 </td>
                        <td class = "edit">     <button class = "btnedit" onclick = "editContact()">Edit</button>       </td>
                        <td class = "delete">   <button class = "btndelete" onclick = "deleteContact()">Delete</button> </td>
                    </tr>                       
                @endforeach
            </tbody>

        </table>
        <br/>
        <a href = "phonebook"><button class = "ret" type = "button">Add New Entry</button></a>
    </body>
</html>

请注意,我已经有了一个按钮来编辑和删除我的联系人,以及功能名称,editContact()deleteContact()

我需要的:

当我点击 "EDIT" 时,我想被重定向到一个使用与 FORM PAGE 相同结构的页面,输入字段填充了我点击 EDIT 按钮的特定行的信息。 一旦我完成编辑条目,我点击 "Submit Information" 按钮,我刚刚编辑的东西 替换 旧的东西, 而不是 创建table 中的新条目编辑:我更喜欢使用Javascript来做这个,所以出现JS的答案没有问题。

删除按钮: 当我点击 "Delete" 按钮时,我希望从我的 table 中删除联系人,并且下一个条目上升一行,取代旧的内容。 编辑:我也更喜欢在这个按钮上使用 JS。

示例: 我在第四排有一个联系人。 我删除这个联系人。 table 中的第五个条目上升一位 因此,它成为 table 中的第四个条目。 同样,第六个条目上升了一个位置,成为第五个条目。 等等。

我已经有了逻辑,但我缺少使功能正常工作的命令。 另外,我对 PHP 和 Laravel.

还很陌生

onclick 属性将尝试使用 javascript,因此如果您尝试使用 PHP,您真的不想这样做。

它可能应该是一个 link(如果您希望它看起来像一个按钮,则风格化为一个按钮)它指向您设置的用于编辑联系人的任何路径。该路由将获取联系人的 ID,然后显示一个表单,其中所有字段都预先填充了该联系人 ID 的信息。

然后表单将指向另一个路由,该路由将获取 ID 和联系信息,并使用编辑表单中输入的所有信息更新数据库中的记录。

删除按钮也是如此,它只需要是一个 link 指向一个路由,该路由设置为使用联系人 ID 删除联系人。然后它可能会重定向回同一页面或之后您希望它去的任何地方。

我会查看资源控制器,它可能会让您的生活更轻松,因为它基本上为您设置了控制器,其中包含 CRUD 操作和路由所需的所有方法。您只需要遍历生成的控制器中的每个方法,并按照您认为合适的方式实现它们。

https://laravel.com/docs/5.4/controllers#resource-controllers

您需要做的是创建一个新页面,该页面将使用您的数据库通过传递 ID 来填写字段。

  • 您需要一个简单的 SELECT 命令来根据您正在编辑的 ID 获取行:SELECT FROM table WHERE ID=yourid。然后通过提交你将不会使用 INSERT INTO table VALUES 你将使用 UPDATE table SET NAME=new_name WHERE ID=yourid

  • 相应的在删除页面,通过ID,使用DELETE FROM table WHERE ID=yourid

如果您不知道如何通过页面传递 ID,可以通过 POSTGET

完成