2016-06-24 20 views
-2

選択オプションでデータベースを選択して、テキストエディタでクエリを実行します。これは私のコードです($データベース== "DATA1")であれば{}JavaScript TypeError:プロパティ 'style' of null phpを読み取ることができません

<div class="form-group"> 
    <label>Editor : </label> 
    <textarea id = "editor" name = "editor" class="form-control" rows="10" placeholder="Enter ..."></textarea> 
</div> 

<div class="col-md-2"> 
<div class="form-group"> 
    <select name="database" id="database" class="form-control select2" style="width: 100%;"> 

    <option value="data1">data1</option> 
    <option value="data2">data2</option> 
    <option value="data3">data3</option> 

</select> 
</div> 
</div> 

    <button type="submit" name="submit" class="btn btn-primary" onclick="executor();">Execute</button> 


    <h3 id = "results">Results</h3> 
    <script> 
     var editor = editor.edit("editor"); 
    </script> 

    <script> 
     function executor() 
     { 
      document.getElementById('cover').style.display = "block"; 
      contentsql = editor.getValue() ; 
      dbvalue = document.getElementById("database").value; 
      $.post("identifier.php", { query: contentsql , database: dbvalue }) 
      .done(function(data) { 
      document.getElementById("results").innerHTML = data ; 
      $('#cover').fadeOut(500); 
     }); 
     } 

     </script> 

コードの

identifier.phpは私がすべての条件のforeachのオプション選択を持っているページですページIdnetifier.php:

require_once ('../../config/connection.php'); 
    function array2table($array, $recursive = false, $null = '&nbsp;') 
    { 

    if (empty($array) || !is_array($array)) { return false;} 
    if (!isset($array[0]) || !is_array($array[0])) { $array = array($array);} 

    $table = " <div id='' class='pull-right'> 
    <form action='/module/getCSV.php' method='post'> 
     <input type='hidden' name='csv_text' id='csv_text'> 
     <input type='submit' value='Export as CSV' onclick='getCSVData()' class='btn btn-success btn-xs'> 
    </form> 
</div> 
<table class='table datatable' id='table_without_sorting' >\n"; 

// The header 
$table .= "\t<tr>"; 
// Take the keys from the first row as the headings 
foreach (array_keys($array[0]) as $heading) { 
    $table .= '<th>' . $heading . '</th>'; 
} 
$table .= "</tr>\n"; 

// The body 
foreach ($array as $row) { 
    $table .= "\t<tr>" ; 
    foreach ($row as $cell) { 
     $table .= '<td>'; 
     $kounter++; 
     // Cast objects 
     if (is_object($cell)) { $cell = (array) $cell; } 

     if ($recursive === true && is_array($cell) && !empty($cell)) {// Recursive mode $table .= "\n" . array2table($cell, true, true) . "\n"; 
     } else { $table .= (strlen($cell) > 0) ?htmlspecialchars((string) $cell) :$null;} 

     $table .= '</td>'; 
     } 

      $table .= "</tr>\n"; 
     } 

     $table .= '</table>'; 
      return $table; 
     } 

    $query = $_POST['query']; 
    $database = $_POST['database']; 
    $sql = $query ; 

    if($database=="data1"){ 
     $request = $co_data1 -> prepare($sql); 
     $request -> execute(); 
     $data_request = $request -> fetchAll(PDO::FETCH_ASSOC); 
    } 

    if($database=="data2"){ 
     $request = $co_data2 -> prepare($sql); 
     $request -> execute(); 
     $data_rreq = $request -> fetchAll(PDO::FETCH_ASSOC); 
     }  

    if($database=="data3"){ 
     $request = $co_data3 -> prepare($sql); 
     $request -> execute(); 
     $data_request = $request -> fetchAll(PDO::FETCH_ASSOC); 
     }  




     echo array2table($data_request); 

     echo "<hr>"; 
     echo "<b>".$kounter."Database: ".$datbase."</b>"; 

     ?> 

ありがとうございます。

+0

identifier.phpファイルも表示できますか?私はそこにあなたを助けなければならないと思う。 – M4R1KU

+0

ありがとうございました。 – user5198335

+0

@ M4R1KU identifier.phpのコードを追加しました。 – user5198335

答えて

0

だから、多くの問題があります。

あなたのサブミット/エグゼキュータボタンは、アクション属性を持つフォームタグ内にあると仮定します。この場合、ボタンをクリックした後、フォームが送信され、リダイレクトを行うため、javascript関数(executor)が完全に実行される機会はありません。

これが当てはまる場合は、このようなフォームのデフォルト動作を禁止する必要があります。

function executor(event) { 
    event.preventDefault(); 
    ... 
} 

とボタン:

<btn... onclick="executor(event)" ...>...</btn> 

とeditor.edit( "編集者")とのあなたの目的は何ですか?それは図書館からですか? この時点まではエディタ変数がないため、エディタは明確に未定義です。

+0

私は何をしたいのですか?リクエスト 。 私のコードがうまく動作せず、エラーが表示されません – user5198335

+0

yコードをpreventdefaultに変更すると、これがエラーです:JavaScript TypeError:プロパティ 'style' of nullを読み取れません – user5198335

関連する問題