2011-12-08 2 views
0

jquery 함수에서 php 함수를 호출하여 일부 값을 계산하고 세션에 값을 저장하고 세션에 저장된 값을 나열하려고합니다. 마우스 클릭 이벤트에서 HTML 폼의 jquery 함수에 값을 전달합니다. 그리고 나는 이것을 달성하기 위해 휴경 코드를 사용하고 있습니다. jquery 함수에서 PHP 함수를 호출하는 방법

하나의 PHP 파일 이름 totalprice.php에서 HTML 양식 코드 :

<table width="100%" > 
<tr> 
      <td >type</td> 
      <td><select name="costtype" id="costype"> 
     <option>one time</option>\n<option>multi</option>\n 
     </select></td></tr> 

     <tr> 
      <td align="right">destination </td> 
      <td> <select name="destination" id ="destination"><option>Lagos</option>\n<option>Oyo</option>\n<option>Kano</option>\n<option>Rivers</option>\n<option>Edo</option>\n<option>Ogun</option>\n<option>Kaduna</option>\n<option>Plateau</option>\n<option>Anambra</option>\n<option>Delta</option></select></td> 
     </tr> 

     <tr> 
      <td align="right">Total eight (Kg) </td> 
      <td><input type="text" size="10" name="weight" id="weight"></td> 
     </tr> 

     <tr> 
      <td>&nbsp;</td> 
      <td><input type="submit" value="Submit"  onclick="javascript:calculate()" name="caliculate" class="button"></td> 
     </tr> 
     </tbody></table> 

     <div id="testtable">cal value will be displayed here.</div> 
</form> 

그리고 이것은 JQuery와 기능

function calculate() { 

    var costtype = $("#costype").val(); 
    var destination = $("#destination").val(); 
    var totalweight = $("#weight").val(); 


    xmlhttp=new XMLHttpRequest(); 

    xmlhttp.onreadystatechange=function() 
     { 
     if (xmlhttp.readyState==4 && xmlhttp.status==0) 
     { 
     document.getElementById("testtable").innerHTML=xmlhttp.responseText; 
     } 
     } 
    xmlhttp.open("POST","caltest.php?item="+escape(costtype)+"&totalweight="+escape(totalweight)+"&destination"+escape(destination),true); 
    xmlhttp.send(); 

    } 

입니다 이것은 테이블

다음
$itemtype = urldecode($_POST['item']); 
$destination = urldecode($_POST['destination']); 
$totalweight = urldecode($_POST['totalweight']); 

$sql  = "select price from prices_list where lower(costtype)='".strtolower($itemtype)."' "; 

$price=0; 
$Result   = mysql_query($sql) ; 

while ($row = mysql_fetch_array($Result)) 
{ 
$price  = (isset($row['price'])) ? $row['price'] : ""; 
} 

$price = round($price,2); 

$costcharges = $price*$totalweight; 
$costtcharges = round($costcharges,2); 

내가
$ fee_details = $ test_fee 세션에 데이터를 저장하는 몇 가지 코드를하고 있어요을 사용하여 내가 세션 데이터를 계산을하고 표시하고있어 경우 파일 caltest.php -> storetestfee ($ itemtype, $ totalweight, $ destination, $ costcharges);

echo "<table width='75%' > 
     <tbody><tr> 
     <th width='2%' class='highlight'><b>#</b></th> 
      <th width='30%' class='highlight'><b>Item </b></th> 
      <th width='23%' class='highlight'><b>Destination</b></th> 
      <th width='5%' class='highlight'><b>weight (Kg) </b></th> 
      <th width='10%' class='highlight'><b>cost Charge</b></th> 
     </tr>"; 
if(count($fee_details)>0) 
{$i=0; 
    foreach($fee_details as $key=>$test_price) 
    {$i++; 
    echo " <tr> "; 
    echo "<td>".$i."</td>"; 
    echo "<td>".$test_price['item_type']."</td>"; 
    echo "<td>".$test_price['destination']."</td>"; 
    echo "<td>".$test_price['totalweight']."</td>"; 
    echo "<td>".$courier_details['weightcharges']."</td>"; 
    echo " </tr> "; 
}} 
echo "</tbody> 
</table>"; 

이 코드는 jquery를 사용하여 직접 작업했을 때 완벽하게 작동합니다. 페이지를 새로 고치지 않고이 작업을 수행하기를 원합니다. 코드에서 실수를 저지르고있는 곳을 도와주세요. 제발. 고맙습니다.

+2

SQL 인젝션 취약점이 있습니다. – SLaks

+0

jQuery를 사용한다면'XMLHttpRequest'를 사용하는 것이 무엇이겠습니까?! – nickb

+0

jQuery AJAX를 사용해야합니다. – SLaks

답변

1

첫 번째, 당신이 jQuery를 사용하는 경우 xmlhttp=new XMLHttpRequest();를 사용할 필요가 없습니다 , jquery는로드, 포스트 또는 아약스를 사용하여이 작업을 수행하는 기능을 가지고 있습니다.

$(function(){ 
    $("input[type=submit]").click(function(){ 
     var costtype = $("#costype").val(); 
     var destination = $("#destination").val(); 
     var totalweight = $("#weight").val(); 

     $.ajax({ 
      type :'POST', 
      url  :"caltest.php?item="+escape(costtype)+"&totalweight="+escape(totalweight)+"&destination"+escape(destination), 
      success :function(result){ 
       //action where request complete, result come from server 
      } 
     }); 
    )}; 
}); 
+0

원 총리, 답장을 보내 주셔서 감사합니다. 이것을 사용하여 계산 된 데이터를 표 형식으로 표시하는 방법을 제안 해 주시겠습니까? – rockingstar

+0

그냥'$ ("some_selector) .html (result)와 같이 서버에서 HTML 결과를 추가하고 싶다면 서버 측에서'caltest.php '결과를 성공 함수로 추가 할 수 있습니다. –

0

당신이 jQuery를 사용하려는 경우, jQuery를 Ajax 기능을 살펴, 나는이 문제를 해결해야한다 같아요 http://api.jquery.com/jQuery.ajax/ 떨어져 모든

관련 문제