【问题标题】:update directly in the same page php直接在同一页面更新php
【发布时间】:2017-10-08 15:49:56
【问题描述】:

我在 update.php 中有这段代码:

public function update($segment,$id){
        $stmt = $this->conn->prepare("UPDATE `segments` SET `segment` = '$segment' WHERE `id` = '$id'") or die($this->conn->error);
        if($stmt->execute()){
            $stmt->close();
            $this->conn->close();
            return true;
        }
    }

在 home.php 我有这个:

 <table   class = "table table-bordered table-responsive  ">
            <thead>

                <th>Segment</th>

                <th>Action</th>
            </thead>
            <tbody>
            <?php

                require 'class.php';

                $conn = new db_class();
                $read = $conn->read();
                while($fetch = $read->fetch_array()){ 

            ?>
                <tr>

                                        <td  contenteditable="true"><?php echo $fetch['segment']?></td>

                            <form action="activate.php" method="post" name="segment"> 
             <textarea style="display: none;" name="segment" id="markup"></textarea>                    
                                   <input type="hidden" name="id" value="<?php echo  $fetch['id']  ;?>">

                                   <td><center><button class = "btn btn-default" name="update"><a href=""></a><span class = "glyphicon glyphicon-edit"></span> Update</button> | <button class = "btn btn-success" type="submit" name="activate"><span class = "glyphicon glyphicon-check"></span> Activate</button> | <button class = "btn btn-danger" name="deactivate"><a href=""></a><span class = "glyphicon glyphicon-folder-close"></span> deactivate</button></center></td>
                            </form>
                </tr>
            <?php
                }
            ?>  
            </tbody>
        </table>

我将其设置为 contenteditable="true" 以直接从同一页面更新,

在激活时我这样做了:

<?php 
require_once 'class.php';

if(ISSET($_POST['update'])){
    $segment =$_POST['segment'];

    $id = $_POST['id'];

    $conn = new db_class();
    $conn->activate($segment, $id);
    echo '
        <script>alert("Updated Successfully")</script>;

    ';
}

?>

所以在 js 中我尝试了这个:

$('#work_form').submit(function(){
// Update the DOM
var block_1 = $('#block_1', '#work_form');
block_1.find('input').each(function(i,e){
    el = $(e);
    el.attr('value', el.val());
});
// Snatch the markup
var markup = block_1.html(); 
// Place it into the textarea
$('#markup', '#work_form').html( markup );
// Move on
return true;

});

当我尝试更新时,我确实回显了查询以查看我得到了这个:

UPDATE `segments` SET `segment` = '' WHERE `id` = '5'

任何帮助都会很棒,非常感谢你

【问题讨论】:

  • 在您的 HTML 中,我看不到任何名称为段的输入字段。这就是为什么你会得到这个未定义的索引。
  • 我不需要任何输入字段,我只想从同一页面进行编辑,我只是​​使 td 标签 contenteditable="true" 这样我可以更改值,但是如何将其保存在数据库中我点击更新@DineshPatra
  • 明白这一点,当你提交表单时,只会提交来自input、textarea、select标签的数据。表单元素列表w3schools.com/html/html_form_elements.asp。虽然您将 td 设置为可编辑的内容,但它不会被提交。
  • 但是要做到这一点,你可以通过ajax提交数据,或者你可以创建一个显示为none的textare。然后在 formsubmit 上,通过 javascript,将 td 标签的 innerHTML 复制到 textarea。将td标签命名为segment,将textarea命名为segment。
  • 我对javascript并不特别,你能告诉我怎么做吗?请?谢谢你@DineshPatra

标签: php mysql oop mysqli


【解决方案1】:

请使用 ajax ,或在某些表单元素中制作您的输入标签。 对于 ajax

var segmant  = $('#id_of_TD').html();
jQuery.ajax({
                    url: 'services/session/token',
                    type: "GET",
                    dataType: "text",
                    data: {
                                'segmant': segmant
                    },
                    success: function (result) {
                         alert('Form is submitted successfully')
                    }
                });

在代码前添加 js 文件并将 url 更改为您的页面名称。

【讨论】:

    【解决方案2】:

    home.php的代码

    <?php 
    require 'class.php';
    $conn = new db_class();
    $read = $conn->read();
    $data = [];
    while($fetch = $read->fetch_array()){ 
        $data[] = $fetch;
    }
    
    ?><!DOCTYPE html>
    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
            <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
        </head>
        <body>
             <table   class = "table table-bordered table-responsive ">
                <thead>
                    <tr>
                        <th>Segment</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($data as $fetch): ?>
                        <tr>
                            <td class="segment" contenteditable="true"><?= $fetch['segment'] ?></td>
                            <td class="text-center">
                                <button class = "btn btn-default action-btn" data-id="<?= $fetch['id'] ?>" data-action="update"> 
                                    <span class = "glyphicon glyphicon-edit"></span> Update
                                </button> 
                                | 
                                <button class = "btn btn-success action-btn" data-id="<?= $fetch['id'] ?>" type="submit" data-action="activate">
                                    <span class = "glyphicon glyphicon-check"></span> Activate
                                </button> 
                                | 
                                <button class = "btn btn-danger action-btn" data-id="<?= $fetch['id'] ?>"  data-action="deactivate">
                                    <span class = "glyphicon glyphicon-folder-close"></span> deactivate
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <script type="text/javascript">
                $(".action-btn").on("click", function(e) {
                    var id      = $(this).attr("data-id");
                    var segment = $(this).parents("tr").find("td.segment").html();
                    var action  = $(this).attr("data-action");
                    $.ajax({
                        "url": "action.php",
                        "method": "post",
                        "data": {
                            "id":      id,
                            "segment": segment,
                            "action":  action
                        },
                        success: function(data) {
                            alert(data);
                        }
                    });
                });
            </script>
        </body>
    </html>
    

    在上面的代码中,根据您的项目结构更改 bootstrap.min.css 的 url。

    现在不要使用activate.php 文件。 创建一个新文件action.php,并添加以下代码。

    <?php 
    
    # If there is no action exit
    if (!isset($_POST['action'])) {
        exit;
    }
    
    $action = $_POST['action'];
    $segment =$_POST['segment'];
    $id = $_POST['id'];
    
    switch($action) {
        case "update":
            $conn = new db_class();
            $conn->activate($segment, $id);
            echo "Updated success fully.";
            exit;
        case "activate":
            # Code for activate
        case "deactivate":
            # Code for deactivate
    
    }
    
    ?>
    

    试试这个。这是通过 ajax 完成的。这里我只为更新功能做了。

    【讨论】:

    • 我试过这个但没有用,所以我只是尝试使用 javascript,但如果你能提供帮助,我会遇到这个错误,那就太好了@
    • 更新工作谢谢你,你能告诉如何进行激活和停用吗? @di
    • 查看action.php,有激活的案例。在那里编写激活代码,就像更新一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多