2013-03-13 86 views
0

我想通過一個jQuery變量使用AJAX的PHP,但它似乎並沒有通過。出於測試目的,我有以下幾點:jQuery的 - 傳遞變量到PHP

JQUERY:

  var runin_pickup = 1200; 

      // Send to our checkRuninTariff function 
      checkRuninTariff(runin_pickup); 

`

function checkRuninTariff(runin_pickup) { 

$.ajax({ 

     // Request sent from control panel, so send to cp.request.php (which is the handler) 
     url: 'scripts/php/bootstrp/all.request.php', 
     type: 'POST', 

     // Build data array - look at the '$_REQUEST' parameters in the 'insert' function 
     data: 'fnme=runIn&field_runin_p='+runin_pickup, 
     dataType: 'text', 
     timeout: 20000, 
     error: function(){ 
       alert("There was a problem...") 
     }, 
     success: function(){ 
       alert(runin_pickup+' passed successfully') 
     } 
    }); 

} 

這被傳遞給all.request.php:

<?php 

include('../../../deployment.php'); 
require_once('controllers/xml.controller.php'); 
require_once('controllers/tariff.fare.controller.php'); 
require_once('controllers/datagrid.controller.php'); 
require_once('controllers/get.bookings.php'); 


// Switch to determine method to call 
switch ($_REQUEST['fnme']) { 

case 'runIn': 
header('Content-type: text/html'); 
echo TariffFareController::getFare($_REQUEST['field_runin_p']); 
break; 

case 'g_fare': 

header('Content-type: text/html'); 

echo TariffFareController::fareRequestHandler($_REQUEST['v_sys'],$_REQUEST['j_dis'],$_REQUEST['pc_arr'], 
    $_REQUEST['leg_arr'],$_REQUEST['return_j'],$_REQUEST['j_date'],$_REQUEST['j_time'], 
    $_REQUEST['r_date'],$_REQUEST['r_time'],$_REQUEST['wait_return_j']); 

break; 
} 

終於到了tariff.fare.controller.php:

public static function getFare($int_terminate,$fieldPickup) { 

     if($fieldPickup > 1100) { 

      $fare = 222.00; 
     } 
     else { 
     $fare = 111.00; 
     } 

} 

在Firebug控制檯我可以看到field_runin_p = 1200

但是,如果我做的var_dump($ fieldPickup);當它應該是1200時,這是NULL。任何想法爲什麼這不起作用?

任何幫助將不勝感激!

回答

1

你的功能需要兩個參數:

getFare($int_terminate, $fieldPickup)

您的代碼然而,僅通過一個(和它不符合你期待是在VAR):

echo TariffFareController::getFare($_REQUEST['field_runin_p']);

0

您正在嘗試從您的PHP - header('Content-type: text/html');發送html,但您要告訴您的javascript期望使用純文本 - dataType: 'text',

改爲使用header('Content-type: text/plain');

其他海報指出您的腳本的其他問題。

我還建議不要使用$_REQUEST,因爲您的數據可能來自GET或POST參數,並且您不知道哪些參數。