2016-06-17 14 views
0

Node.js + Express.jsを使用して、ライブラリデータベース用の非常に基本的な検索エンジンを構築しています。私のHTMLには書籍や書籍のMySQLデータベースを照会するためにタイトルやキーワードをアプリに送るPOSTフォームがあります。タイトルによる検索は正常に機能していますが、キーワードでは苦労しています。下記参照。HTML POST要求がすべてのデータを送信しない

HTML

<form id="searchForm" action="" method="post"> 

     <select class="form-control" id="searchType"> 
      <option class="" value="0">Search by title</option> 
      <option class="" value="1">Search by keyword</option> 
     </select> 

     <select class="form-control" id="titles" name="titles"> 
      <% for (var title in titles) {;%> 

      <option class=""><%=titles[title].TITLE%></option> 

      <% }; %> 
     </select> 

     <textarea class="form-control" name="keyword" contenteditable="false" id="keyword" placeholder="Enter keyword here..."></textarea> <!-- placeholder is only supported in a few browesers 
                                      (Firefox 3.7+, Chrome, Safari, IE 10). Could use 
                                      jQuery but ah--> 
     <input class="btn btn-default" type="submit" value="Search"></input> 
    </form> <!-- /searchForm --> 

エクスプレスコード

app.post('/getBooksByTitle', function (req, res) { 
    connection.getConnection(function(err, tempCon) { 
     if (err) { 
      tempCon.release(); 
      console.log('ERROR IN SQL CONNECTION!'); 
     } 
     else { 
      console.log('CONNECTED TO SQL'); 
      tempCon.query('SELECT * FROM BOOKS WHERE TITLE=?', req.body.titles, function(err, rows) { 
       if (err) { 
        console.log('BAD QUERY'); 
       } 
       else { 
        console.log(req.body.titles); 
        res.json(rows); 
       } 
       tempCon.release(); 
      }); 
     } 
    }); 
}); 
app.post('/getBooksByKeyword', function (req, res) { 
    connection.getConnection(function(err, tempCon) { 
     if (err) { 
      tempCon.release(); 
      console.log('ERROR IN SQL CONNECTION!'); 
     } 
     else { 
      console.log('CONNECTED TO SQL'); 
      tempCon.query('SELECT * FROM BOOKS WHERE (AUTHOR LIKE ? || TITLE LIKE ? || GENRE LIKE ? || DESCRIPTION LIKE ?)', '%' + req.body.keyword + '%', function(err, rows) { 
       if (err) { 
        console.log('BAD QUERY'); 
        console.log(req.body); 
       } 
       else { 
        res.json(rows); 
        console.log(req.body.keyword); 
       } 
       tempCon.release(); 
      }); 
     } 
    }); 
}); 

私はreq.body.(field_name)してノードへのフォームデータを引っ張っていますが、テキストボックスを収集していないようです。もし私がconsole.log(req.body)私はタイトルフィールドのみを参照してください。私はどこを台無しにしましたか?

EDIT:アクションといくつかのアニメーションを処理するjQueryスクリプト。

$(document).ready(function() { 
    toggleFields(); //call this first so we start out with the correct visibility depending on the selected form values 
    //this will call our toggleFields function every time the selection value of our searchType field changes 
    $("#searchType").change(function() { 
     toggleFields(); 
    }); 

}); 
//this toggles the visibility of our input fields depending on the current selected value of the searchType field. 
//also it toggles the action of the submit form to the appropriete post call. 
function toggleFields() { 
    if ($("#searchType").val() == 0) { 
     $("#searchForm").attr('action', '/getBooksByTitle'); 
     $("#titles").slideDown(); 
     $("#keyword").slideUp(); 
    } 
    else { 
     $("#searchForm").attr('action', '/getBooksByKeyword'); 
     $("#titles").slideUp(); 
     $("#keyword").slideDown(); 
    } 
} 
+0

devtoolsの[ネットワーク]タブを確認します。データは送信されますか? – Jeff

+0

@Jeff FirebugでNetタブをチェックすると、タイトルフィールドのみが送信されたように見えます。 – Akaitenshi

+0

あなたのhtmlで両方のルートにデータを投稿してもらいましたか? – Elyas74

答えて

0

ご協力いただきありがとうございます。 <textarea>フィールドを<input>フィールドに変更して問題を解決しました。今、投稿は私のタイトルとキーワードの両方をサーバーに送り返します。

関連する問題