2014-10-06 3 views
-2

안녕 난 내가 여기 확인란에서 데이터베이스에서 값을 검색하는 방법?

데이터베이스

에서 검색해야하는 체크 박스 값에 대한 선택이 어느 확인란을 원하는 세 가지 체크 박스를하는 것은

<input type="checkbox" name="test" value="X-Ray" style="margin-top:10px;margin-left:120px;"><label>X-Ray</label> 
<input type="checkbox" name="test" value="Ecg" style="margin-top:10px;margin-left:20px;"><label>ECG</label> 
<input type="checkbox" name="test" value="Blood Test" style="margin-top:10px;margin-left:20px;"><label>Blood Test</label> 

MySQL은 쿼리 내 확인란

입니다
SELECT SUM(price) from test where test='x-ray' or test='' or test='bloodtest' 

원하는 출력을 얻으려면 어떻게해야합니까? 어떤 도움을 주시면 감사하겠습니다.

+0

value? –

+1

@OP : 사람들이 준 답변을 확인하는 데 조심합니까? 아니, 이거 니가? –

답변

1

jquery 선택기 :checked을 사용하면 특정 input 체크 박스를 누르고있을 수 있습니다. 당신을 얻을해야 자바 스크립트에서이 같은 일이 시작 :

$("input").on("click", function() { 
    var sel = $("input:checked").val(); 
     //Here you can just make a simple ajax request to a php script passing the 
     //specific checkbox value and let that script perform the mysql query. 
     $.post("test.php", { test: sel }) 
     .done(function(data) { 
     alert("Completed"); 
     }); 
}); 

귀하의 test.php 스크립트는 다음과 같다 :

<?php 
    $test = $_POST["test"]; 

    //Replace with your sql database credentials 
    $con=mysqli_connect("example.com","peter","abc123","my_db"); 

    // Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$result = mysqli_query($con,"SELECT SUM(price) from test where test='".$test."'"); 
mysqli_close($con); 
?> 

이것은 당신이 당신의 문제를 진행할 수 있는지의 템플릿을 시작하는 베어 본이다. 물론 구체적인 사용 사례는 다양 할 수 있습니다. 예를 들어 post 요청 대신 get 요청을하고 PHP 스크립트가 상호 작용하고 데이터를 다르게 가져올 수 있습니다.

방금 ​​jquery와 php에서 워크 플로가 어떻게 생겼는지 예를 들었습니다. 따라서 입력 체크 박스의 값을 가져 와서 데이터베이스와 상호 작용하는 스크립트에 값을 전달하고 특정 SUM을 가져옵니다. 이 문서를 더 잘 읽으려면 Jquery Ajax 또는 PHP Mysql에 대한 설명서를 읽어야합니다. 희망이 도움이됩니다.

+1

누군가가 대답이 왜 왜곡되었는지 설명해 주시면 고맙겠습니다. –

-1

나는이 최선의 해결책은 출력하는 것입니다. 모두 페이지의 어딘가에서 JavaScript 변수로 가격이 책정되었으므로 조금 HTML을 변경합시다.

<input type="checkbox" class="chkbox-update" name="test" value="X-Ray"><label>X-Ray</label> 
<input type="checkbox" class="chkbox-update" name="test" value="Ecg"><label>ECG</label> 
<input type="checkbox" class="chkbox-update" name="test" value="Blood Test"><label>Blood Test</label> 

이제 가격. 결과를 통해 itterate과 JSON 형식의 변수를 구성하는 PDO를 사용

<script> 
var prices = {"X-ray": 3900, "ECG": 2000, "Blood Test": 1200}; 
</script> 

는 그럼 난 이것에 대한 jQuery를 사용하고, 가격 필드를 업데이트 자바 스크립트를 사용합니다.

$('.chkbox-update').click(function() { 
    var total; 
    $.each($('.chkbox-update'), function(k,v) { 
     total += prices[$(this).val()]; 
    }); 
    $('#result').text('The total price is '+total); 
}); 

prices 변수의 키가 일치하는지 확인을 확인 내가 서버 측 스크립트를 볼 수없는 이유 <input>

관련 문제