2017-04-20 3 views
1

MySQL 테이블에서 데이터를 검색하고 검색하려고합니다. 검색 키워드를 얻기 위해 두 개의 텍스트 필드를 사용했습니다. 나는 이것을 위해 jquery ajax를 사용했다. 처음으로 검색을 위해 위치 만 사용했습니다. 그런 다음 작동합니다. 두 텍스트 필드를 모두 사용하면 효과가 없습니다. 그렇다면 나는 항상 no data입니다.jquery ajax에서 두 개의 텍스트 필드 데이터를 사용하는 검색 기능

var loca=$('#location').val(); 
var cato=$('#catogary').val(); 

는 또한 dataType하지 DataType를 사용할

<div class="container"> 
<form action="search.php" method="post"> 
    <input type="text" id="location" name="location">&nbsp; 
    <input type="text" id="catogary" name="catogary"> 
    <script> 
     $('#location,#catogary').keyup(function() { 
      var loca=$(this).val(); 
      var cato=$(this).val(); 
      if(loca!='' || cato!=''){ 
       $.ajax({ 
        url:"search.php", 
        method:"POST", 
        data:{searc:loca,cato:cato}, 
        DataType:"text", 
        success:function (data) { 
         $('#result').html(data); 
        } 
       }); 
      } 
      else{ 
       $('#result').html(''); 
      } 

     }); 
    </script> 
</form> 
<div id="result"></div> 
</div> 

PHP 코드

<?php 

    $conn=mysqli_connect("localhost","root","","internship"); 
    $output=''; 
    $sql="select * from vacancy where location like '%".$_POST["searc"]."%' 
    and catogary like '%".$_POST["cato"]."%'"; 
    $res=mysqli_query($conn,$sql); 
    if(mysqli_num_rows($res)>0){ 

    while($row=mysqli_fetch_assoc($res)){ 
    ?> 
     <div class="bs-calltoaction bs-calltoaction-primary"> 
        <div class="row"> 
         <div class="col-md-9 cta-contents"> 
          <h1 class="cta-title">Its a Call To Action</h1> 
          <div class="cta-desc"> 
    <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br> 
    <input type="text" value='<?= $row['company_name'];?>' readonly style="width: 75%"><br><br> 
    <input type="text" value='<?= $row['location'];?>' readonly style="width: 75%"><br><br> 
    <input type="text" value='<?= $row['qulification'];?>' readonly style="width: 75%"><br><br> 
    <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br> 
    <input type="text" value='<?= $row['indate'];?>' readonly style="width: 37.5%">&nbsp; 
    <input type="text" value='<?= $row['expdate'];?>' readonly style="width: 37.5%"> 
       </div> 

         </div> 
         <div class="col-md-3 cta-button"> 
          <a class="btn btn-primary btn-large" 
        href="#myModal" data-toggle="modal">Apply for job</a> 
         </div> 
        </div> 
       </div> 
      <?php 
     } 



      } 
      else{ 
      echo 'no data'; 
      } 

      ?> 
+1

값 얻기 위해, 살균 및 및 유효성을 검사 한 후 사용한다. –

+0

ajax 호출에서 두 필드의 값이 아니라 loca와 cato를 같은 것으로 설정합니다. –

답변

2

변경 아래 2 라인에 의해

var loca=$(this).val(); 
var cato=$(this).val(); 

, .,

$.ajax({ 
    url:"search.php", 
    method:"POST", 
    data:{searc:loca,cato:cato}, 
    dataType:"text", // use dataType not DataType 
    success:function (data) { 
     $('#result').html(data); 
    } 
}); 
+0

대단히 고맙습니다.이 작품은 100 %. 당신의 페이스 북 이름을 알려주세요. –

+0

답변이 제대로 작동하므로 승인 된 것으로 표시하십시오. –

+0

@DanithKumarasinghe 필요한 이유는 SO 프로필 페이지가 부족합니다 :) –

0

사용 VAR의 삶을 = $ ('# 위치') 발(); var cato = $ ('# catogary') .val();

가 입력

사용하면 만족이 조건은 또한 쿼리에서 직접 게시 데이터를 사용하지 않는 데이터를 모두이 없을 수도 있습니다이 쿼리

$sql="select * from vacancy"; 
    $where = ""; 

    if(!empty($_POST["searc"]){ 
     if($where == "") { 
      $where = $where." where location like '%".$_POST["searc"]."%'"; 
     } else { 
      $where = $where." AND location like '%".$_POST["searc"]."%'"; 
     } 
    } 
    if(!empty($_POST["cato"]){ 
     if($where == "") { 
      $where = $where." where catogary like '%".$_POST["cato"]."%'"; 
     } else { 
      $where = $where." AND catogary like '%".$_POST["cato"]."%'"; 
     } 
    } 

    $sql = $sql.$where; 
관련 문제