2017-05-10 7 views
0

に私はupdate.phpをこのコードがあります。更新は直接、同じページの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に私はこれを持っています「同じページから直接アップデートするには、

とアクティブに私はこれでした:

<?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' 

を、home.phpのために多く

+0

あなたのHTMLでは、名前がセグメントである入力フィールドは表示されません。なぜこの未定義のインデックスを取得しているのですか? –

+0

入力フィールドは必要ありません。同じページから編集したいのですが、値を変更することができるようにcontentditable = "true"というtdタグを作成しますが、@DineshPatraを更新するとデータベースに保存する方法 – eddy

+0

フォームを送信すると、input、textarea、selectタグのデータのみが送信されます。フォーム要素https://www.w3schools.com/html/html_form_elements.aspのリスト。 コンテンツを編集可能な状態にしても、それは送信されません。 –

答えて

0

コードUに感謝

<?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を介して行われます。ここでは更新機能のみを行っています。

+0

私はこれを試して、私はちょうどjavascriptを使用しようとしましたが、私はそれが素晴らしいだろうと助けることができる場合はこのエラーがあります@ – eddy

+0

更新作品はあなたに感謝uは、無効にしますか? @di – eddy

+0

action.phpを参照してください。有効にする場合があります。アップデートのようにそこでアクティブにするためのコードを書いてください。 –

1

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') 
        } 
       }); 

ため は、ページ名にコードやURLの変更前のjsファイルを追加します。

関連する問題