2017-08-04 6 views
0

ボタンをクリックするとMy Modalが起動しません。私はすべての可能なアプローチを試してみました。すべての可能な方法を試しました。「ヒット&試してみてください。だから私はこのフォーラムに戻って助けを求める。MySQLからのデータの入力がModalフォームで行われていない

Main.php次のように

<!DOCTYPE html> 
<html> 
     <head> 
      <title>Webslesson Tutorial | Bootstrap Modal with Dynamic MySQL Data using Ajax & PHP</title> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
     </head> 
     <body> 
      <br /><br /> 
      <div class="container" style="width:700px;"> 
       <h3 align="center">Bootstrap Modal with Dynamic MySQL Data using Ajax & PHP</h3> 
       <br /> 
       <div class="table-responsive"> 
        <table id="recordsfromraky" class="table table-striped table-bordered">   
     </table> 
       </div> 
      </div> 
     </body> 
</html> 
<div id="dataModal" class="modal fade"> 
     <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal">&times;</button> 
        <h4 class="modal-title">Employee Details</h4> 
       </div> 
       <div class="modal-body" id="employee_detail"> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
     </div> 
</div> 
<script> 
$(document).ready(function(){ 
     fetchChange(); 
     $('.view_data').click(function(){ 
      var employee_id = $(this).attr("id"); 
      var fired_button = $(this).val(); 
      alert(fired_button); 

        $('#dataModal').modal("show"); 

     }); 
     function fetchChange() { 
       var finyear = "2017-18"; 
       var cmdname = "HQ TC"; 
       var stnname = "All Stations"; 
       var statusname = "All Status"; 
       var cfaname  = "All CFA"; 
       var wkcatname = "All Categories"; 

       $.ajax({ 
         url:"get_value.php", 
         method:"POST", 
         data:{finyear:finyear, wkcatname:wkcatname,cmdname:cmdname,stnname:stnname, statusname:statusname,cfaname:cfaname}, 
         success:function(data){ 
          $('#recordsfromraky').html(data); 
         } 
       }); 
      }; 
}); 
</script> 

get_value.phpコードがある: - - :

<?php 
    ob_start(); 
    include_once $_SERVER['DOCUMENT_ROOT'] . '/CommandWks/phpAssets/dbconnect.php'; 
    ini_set("xdebug.var_display_max_children", -1); 
    ini_set("xdebug.var_display_max_data", -1); 
    ini_set("xdebug.var_display_max_depth", -1);  

    $finyr   = $_POST['finyear']; 
    $commdname  = $_POST['cmdname']; 
    $stationname = $_POST['stnname']; 
    $statustype  = $_POST['statusname']; 
    $cfanamed  = $_POST['cfaname']; 
    $catname  = $_POST['wkcatname']; 

    $finyr   = str_replace('"', '', $finyr); 
    $commdname  = str_replace('"', '', $commdname); 
    $stationname = str_replace('"', "'", $stationname); 
    $statustype  = str_replace('"', "", $statustype); 
    $cfanamed  = str_replace('"', '', $cfanamed); 
    $catname  = str_replace('"', '', $catname); 

    //var_dump($_POST);//////////////////////////////////////// 

    $wherefy =''; 
    $wherecmd =''; 
    $wherestn = ''; 
    $wherestatus =''; 
    $wherecfa =''; 
    $wherecat =''; 
    $tcn=''; 

    if (empty($finyr)){ 
     $wherefy = ''; 
    }else{           
     $tcn = "'" . '%' . str_replace(' ', '', $finyr) . '%' . "'"; 
     $wherefy = 'WHERE (`amwplist`.`Work_ID_by_MoD` LIKE ' . $tcn . ')'; 
    } 

    if (!empty($stationname)) { 
     if ($stationname === "All Stations"){ 
      $wherestn=''; 

     }else{ 
      $tcn=''; 
      $tcn = "'" . str_replace("''", "", $stationname) . "'"; 
      $wherestn = 'AND (`stationlist`.`Station` = ' . $tcn . ')'; 

     } 
    }else{   
     $wherestn=''; 
    } 

    if (!empty($commdname)) { 
     if ($commdname === "All Commands"){ 
      $wherecmd=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. str_replace(' ', '', $commdname) . '%' . "'"; 
      $wherecmd = 'AND (`amwplist`.`Work_ID_by_MoD` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherecmd=''; 
    } 

    if (!empty($statustype)) { 
     if ($statustype === "All Status"){ 
      $wherestatus=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. $statustype . '%' . "'"; 
      $wherestatus = 'AND (`workstatuslist`.`Status_Type` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherestatus=''; 
    } 

    if (!empty($cfanamed)) { 
     if ($cfanamed === "All CFA"){ 
      $wherecfa=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. $cfanamed . '%' . "'"; 
      $wherecfa = 'AND (`cfalist`.`CFA` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherecfa=''; 
    } 

    if (!empty($catname)) { 

     if ($catname === "All Categories"){ 
      $wherecat=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. $catname . '%' . "'"; 
      $wherecat = 'AND (`workscategorylist`.`Category_Type` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherecat=''; 
    } 

     $WhereCondition = $wherefy . " " . $wherestatus . " " . $wherestn . " " . $wherecfa . " " . $wherecat . " " . $wherecmd; 

     //var_dump($WhereCondition); 
     //var_dump($wherefy); 
     //var_dump($wherecmd); 
     //var_dump($wherestn); 
     //var_dump($wherestatus); 
     //var_dump($wherecfa); 
     //ovar_dump($wherecat); 


     $sql ="SELECT 
      * 
      FROM `amwplist` 
      LEFT OUTER JOIN `workscategorylist` ON `amwplist`.`Category_ID` = `workscategorylist`.`Category_ID` 
      INNER JOIN `stationlist` ON `amwplist`.`Station_ID` = `stationlist`.`Station_ID` 
      LEFT OUTER JOIN `commandhqlist` ON `stationlist`.`Command_ID` = `commandhqlist`.`CommandHQ_ID` 
      LEFT OUTER JOIN `sectorlist` ON `stationlist`.`Sector_ID` = `sectorlist`.`Sector_ID` 
      LEFT OUTER JOIN `celist` ON `stationlist`.`CE_ID` = `celist`.`CE_ID` 
      LEFT OUTER JOIN `cwelist` ON `stationlist`.`CWE_ID` = `cwelist`.`CWE_ID` 
      LEFT OUTER JOIN `gelist` ON `stationlist`.`GE_ID` = `gelist`.`GE_ID` 
      LEFT OUTER JOIN `ifalist` ON `amwplist`.`IFA_ID` = `ifalist`.`IFA_ID` 
      LEFT OUTER JOIN `cdalist` ON `stationlist`.`CDA_ID` = `cdalist`.`CDA_ID` 
      LEFT OUTER JOIN `cfalist` ON `amwplist`.`CFA_ID` = `cfalist`.`CFA_ID` 
      LEFT OUTER JOIN `workstatuslist` ON `amwplist`.`Status_ID` = `workstatuslist`.`Status_ID` " . 
      $WhereCondition . " " . 
      "ORDER BY substring_index(amwplist.Work_ID_by_MoD, '/', 1), CONVERT(substring_index(amwplist.Work_ID_by_MoD, '/', -1), UNSIGNED INTEGER) 
      "; 

      $tableRow = '<thead>  
        <tr>   
         <th>Category Type</th>   
         <th>Station</th>      
         <th>MoD ID</th>     
         <th>Nomenclature</th>     
         <th>Cost</th>     
         <th>Status</th>     
        </tr>  
       </thead>  
        <tbody>'; 
      $conn = new mysqli($DBSERVER, $DBUSER, $DBPASS, $DBNAME); 
      $result = mysqli_query($conn, $sql); 
     //var_dump($conn); 
      while ($row= mysqli_fetch_array($result)) { 
       //$idnumber = CONVERT(substring_index($row['Work_ID_by_MoD'], '/', -1), UNSIGNED INTEGER); 
       $tableRow = $tableRow . '<tr>' ; 
       $tableRow = $tableRow . '<td>'. $row['Category_Type'] . '</td>'; 
       $tableRow = $tableRow . '<td>'. $row['Station'] . '</td>'; 
       $trow = $row['Work_ID_by_MoD']; 
       //var_dump($trow); 
       //var_dump('<td><input type="button" name= "' . $row['Work_ID_by_MoD'] . '" value= "' . $row['Work_ID_by_MoD'] . '" id= "' . $row['Work_ID_by_MoD'] . '" class="btn btn-info btn-xs view_data" /></td>'); 
       $tableRow = $tableRow . '<td><input type="button" name= "' . $trow . '"' . 'value= "' . $trow . '" id= "' . $trow . '" class="btn btn-info btn-xs view_data" /></td>'; 

       $tableRow = $tableRow . '<td>'. $row['Nomenclature'] . '</td>'; 
       $tableRow = $tableRow . '<td>'. $row['Cost'] . '</td>'; 
       $tableRow = $tableRow . '<td>'. $row['Status_Type'] . '</td>'; 
       $tableRow = $tableRow . '</tr>'; 

      }   
      $tableRow = $tableRow . '</tbody>'; 
      //var_dump($tableRow); 
    echo ($tableRow); 
    $conn->close(); 
    ob_end_flush(); 

?> 

関数.click()第三に国防省IDをクリックすることで、私のコードは、以下であります列が発射されていません。私は今、テーブルにはmain.phpにget_value.phpによって満たされていると、私は火にしようとしていますので、多分ここでハンドルの問題があると思い

<input type="button" name="AMWP/2017-18/1" value="AMWP/2017-18/1" id="AMWP/2017-18/1" class="btn btn-info btn-xs view_data">    
<input type="button" name="AMWP/2017-18/2" value="AMWP/2017-18/2" id="AMWP/2017-18/2" class="btn btn-info btn-xs view_data"> 
<input type="button" name="AMWP/2017-18/3" value="AMWP/2017-18/3" id="AMWP/2017-18/3" class="btn btn-info btn-xs view_data"> 

- :この列の行の内容は、次のようになりますget_value.phpで生成されたボタンをクリックしてモーダルにします。

+0

テーブルが 'main.php'のテーブルを埋めるためにajax関数を呼び出すことで、テーブルがいっぱいになるため、ハンドルの問題だと思います。テーブルがハードコードされているか、ajax関数を呼び出してテーブルを埋めることなくコードが正常に動作します。 'get_value.php'を使って塗りつぶしてから、' main.php'にButtonのHandlesを追加する方法があります。ですから、もし誰かが私に 'main.php'の物語を記入してください。これはajaxコールから' get_value.php'へのデータを取得し、ボタンをクリックしてModalを開くことができます。 –

+0

私はモーダルを呼び出すためにアヤックスをどこに配置するかとは何かを感じます。ページが既に準備されていてボタンが追加されているので、おそらく外部の$(document)、ready()です。多分、そうでないかもしれない。しかし、助けてください。 –

+0

別の注記:MySQLのクエリでPOST値を使用するときは、実際にそれを消毒しなければならない、またはあなたが注射に開放されている必要があります。 http://php.net/manual/en/function.mysql-real-escape-string.phpを参照してください – masterfloda

答えて

0

問題は、既存のオブジェクト以外にクリックイベントをバインドしようとしていることです。つまり、$('.view_data').click(...が発生して、ajax呼び出しが終了し、したがって、クラスがview_dataの要素が存在しません。

1)ボタンのイベントを結合するが、bodyではありません:

2つのオプションがあります。私はイベントをバインドしないが、委任を使用するので、私は個人的にこのメソッドを好む。それは(あなたが一つの文書内のイベントの多くを持つ終わる場合は特に)、デバッグが容易だし、維持する:あなたが結果を書き込むAFTER AJAX呼び出しの成功方法で結合事象を入れ

$('body').on('click', '.view_data', function(){ 
    alert('clicked ' + $(this).val()); 
}); 

2) DOM

fetchChange(); 

    function fetchChange() { 
      var finyear = "2017-18"; 
      var cmdname = "HQ TC"; 
      var stnname = "All Stations"; 
      var statusname = "All Status"; 
      var cfaname  = "All CFA"; 
      var wkcatname = "All Categories"; 

      $.ajax({ 
        url:"get_value.php", 
        method:"POST", 
        data:{finyear:finyear, wkcatname:wkcatname,cmdname:cmdname,stnname:stnname, statusname:statusname,cfaname:cfaname}, 
        success:function(data){ 
         $('#recordsfromraky').html(data); 

         $('.view_data').click(function(){ 
          var employee_id = $(this).attr("id"); 
          var fired_button = $(this).val(); 
          alert(fired_button); 

          $('#dataModal').modal("show"); 

         }); 
        } 
      }); 
     }; 

});次のように

+0

はい、これは動作します。私はここにハンドルと関係があることを知っていました。私は別の仕事を見つけました。そこで、ループの最後に$ tablerowに.clickのスクリプトを追加してから、テーブルのデータをmain.phpに戻しました。それも機能します。もう一度おねがいします。 –

0

改正get_value.phpは次のとおりです。 -

<?php 
    ob_start(); 
    include_once $_SERVER['DOCUMENT_ROOT'] . '/CommandWks/phpAssets/dbconnect.php'; 
    ini_set("xdebug.var_display_max_children", -1); 
    ini_set("xdebug.var_display_max_data", -1); 
    ini_set("xdebug.var_display_max_depth", -1);  

    $finyr   = $_POST['finyear']; 
    $commdname  = $_POST['cmdname']; 
    $stationname = $_POST['stnname']; 
    $statustype  = $_POST['statusname']; 
    $cfanamed  = $_POST['cfaname']; 
    $catname  = $_POST['wkcatname']; 

    $finyr   = str_replace('"', '', $finyr); 
    $commdname  = str_replace('"', '', $commdname); 
    $stationname = str_replace('"', "'", $stationname); 
    $statustype  = str_replace('"', "", $statustype); 
    $cfanamed  = str_replace('"', '', $cfanamed); 
    $catname  = str_replace('"', '', $catname); 

    //var_dump($_POST);//////////////////////////////////////// 

    $wherefy =''; 
    $wherecmd =''; 
    $wherestn = ''; 
    $wherestatus =''; 
    $wherecfa =''; 
    $wherecat =''; 
    $tcn=''; 

    if (empty($finyr)){ 
     $wherefy = ''; 
    }else{           
     $tcn = "'" . '%' . str_replace(' ', '', $finyr) . '%' . "'"; 
     $wherefy = 'WHERE (`amwplist`.`Work_ID_by_MoD` LIKE ' . $tcn . ')'; 
    } 

    if (!empty($stationname)) { 
     if ($stationname === "All Stations"){ 
      $wherestn=''; 

     }else{ 
      $tcn=''; 
      $tcn = "'" . str_replace("''", "", $stationname) . "'"; 
      $wherestn = 'AND (`stationlist`.`Station` = ' . $tcn . ')'; 

     } 
    }else{   
     $wherestn=''; 
    } 

    if (!empty($commdname)) { 
     if ($commdname === "All Commands"){ 
      $wherecmd=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. str_replace(' ', '', $commdname) . '%' . "'"; 
      $wherecmd = 'AND (`amwplist`.`Work_ID_by_MoD` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherecmd=''; 
    } 

    if (!empty($statustype)) { 
     if ($statustype === "All Status"){ 
      $wherestatus=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. $statustype . '%' . "'"; 
      $wherestatus = 'AND (`workstatuslist`.`Status_Type` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherestatus=''; 
    } 

    if (!empty($cfanamed)) { 
     if ($cfanamed === "All CFA"){ 
      $wherecfa=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. $cfanamed . '%' . "'"; 
      $wherecfa = 'AND (`cfalist`.`CFA` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherecfa=''; 
    } 

    if (!empty($catname)) { 

     if ($catname === "All Categories"){ 
      $wherecat=''; 
     }else{ 
      $tcn=''; 
      $tcn = "'" . '%'. $catname . '%' . "'"; 
      $wherecat = 'AND (`workscategorylist`.`Category_Type` LIKE ' . $tcn . ')'; 
     } 
    }else{   
     $wherecat=''; 
    } 

     $WhereCondition = $wherefy . " " . $wherestatus . " " . $wherestn . " " . $wherecfa . " " . $wherecat . " " . $wherecmd; 

     //var_dump($WhereCondition); 
     //var_dump($wherefy); 
     //var_dump($wherecmd); 
     //var_dump($wherestn); 
     //var_dump($wherestatus); 
     //var_dump($wherecfa); 
     //ovar_dump($wherecat); 


     $sql ="SELECT 
      * 
      FROM `amwplist` 
      LEFT OUTER JOIN `workscategorylist` ON `amwplist`.`Category_ID` = `workscategorylist`.`Category_ID` 
      INNER JOIN `stationlist` ON `amwplist`.`Station_ID` = `stationlist`.`Station_ID` 
      LEFT OUTER JOIN `commandhqlist` ON `stationlist`.`Command_ID` = `commandhqlist`.`CommandHQ_ID` 
      LEFT OUTER JOIN `sectorlist` ON `stationlist`.`Sector_ID` = `sectorlist`.`Sector_ID` 
      LEFT OUTER JOIN `celist` ON `stationlist`.`CE_ID` = `celist`.`CE_ID` 
      LEFT OUTER JOIN `cwelist` ON `stationlist`.`CWE_ID` = `cwelist`.`CWE_ID` 
      LEFT OUTER JOIN `gelist` ON `stationlist`.`GE_ID` = `gelist`.`GE_ID` 
      LEFT OUTER JOIN `ifalist` ON `amwplist`.`IFA_ID` = `ifalist`.`IFA_ID` 
      LEFT OUTER JOIN `cdalist` ON `stationlist`.`CDA_ID` = `cdalist`.`CDA_ID` 
      LEFT OUTER JOIN `cfalist` ON `amwplist`.`CFA_ID` = `cfalist`.`CFA_ID` 
      LEFT OUTER JOIN `workstatuslist` ON `amwplist`.`Status_ID` = `workstatuslist`.`Status_ID` " . 
      $WhereCondition . " " . 
      "ORDER BY substring_index(amwplist.Work_ID_by_MoD, '/', 1), CONVERT(substring_index(amwplist.Work_ID_by_MoD, '/', -1), UNSIGNED INTEGER) 
      "; 

      $tableRow = '<thead>  
        <tr>   
         <th>Category Type</th>   
         <th>Station</th>      
         <th>MoD ID</th>     
         <th>Nomenclature</th>     
         <th>Cost</th>     
         <th>Status</th>     
        </tr>  
       </thead>  
        <tbody>'; 
      $conn = new mysqli($DBSERVER, $DBUSER, $DBPASS, $DBNAME); 
      $result = mysqli_query($conn, $sql); 
     //var_dump($conn); 
      while ($row= mysqli_fetch_array($result)) { 
       //$idnumber = CONVERT(substring_index($row['Work_ID_by_MoD'], '/', -1), UNSIGNED INTEGER); 
       $tableRow = $tableRow . '<tr>' ; 
       $tableRow = $tableRow . '<td>'. $row['Category_Type'] . '</td>'; 
       $tableRow = $tableRow . '<td>'. $row['Station'] . '</td>'; 
       $trow = $row['Work_ID_by_MoD']; 
       //var_dump($trow); 
       //var_dump('<td><input type="button" name= "' . $row['Work_ID_by_MoD'] . '" value= "' . $row['Work_ID_by_MoD'] . '" id= "' . $row['Work_ID_by_MoD'] . '" class="btn btn-info btn-xs view_data" /></td>'); 
       $tableRow = $tableRow . '<td><input type="button" name= "' . $trow . '"' . 'value= "' . $trow . '" id= "' . $trow . '" class="btn btn-info btn-xs view_data" /></td>'; 

       $tableRow = $tableRow . '<td>'. $row['Nomenclature'] . '</td>'; 
       $tableRow = $tableRow . '<td>'. $row['Cost'] . '</td>'; 
       $tableRow = $tableRow . '<td>'. $row['Status_Type'] . '</td>'; 
       $tableRow = $tableRow . '</tr>'; 

      }   
      $tableRow = $tableRow . '</tbody>'; 

      $tableRow = $tableRow . "<script>$('.view_data'" . ").click(function(){ 
                var employee_id = $(this).attr('id'); 
                var fired_button = $(this).val(); 
                alert(fired_button); 

               $('#dataModal').modal('show'); 

               }); 
             </script>"; 
      //var_dump($tableRow); 
    echo ($tableRow); 
    $conn->close(); 
    ob_end_flush(); 

?> 

してくださいませんループの終わりの添付。それもトリックでした。

+0

これはかなり汚い解決策です。あなたのJSを1つの場所に保管してください(一般的には、インラインJSを避けて.jsファイルに入れてください)。また、イベントの委任(例: '。on() ')を直接イベントバインディングの代わりに使用します。 – masterfloda

+0

はい、あなたの解決策はより良いと良いです。間違いない。私が見せようとしているのは、代わりの方法ですが、良いコーディング方法ではありません。 –

関連する問題