2016-06-17 3 views
0

Node.js + Express.js를 사용하여 라이브러리 데이터베이스에 대한 매우 기본적인 검색 엔진을 구축하고 있습니다. 내 HTML에는 book/books에 대한 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> 필드로 변경하여 문제를 해결했습니다. 이제 게시물은 내 제목과 키워드를 모두 서버로 보냅니다.

관련 문제