[英]jQuery - Passing variable to PHP
I am trying to pass a jQuery variable to PHP using AJAX but it does not seem to be passing. 我试图使用AJAX将jQuery变量传递给PHP,但似乎没有传递。 For testing purposes I have the following:
出于测试目的,我具有以下内容:
JQUERY: 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')
}
});
}
This gets passed to all.request.php: 这将传递给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;
}
And finally to the tariff.fare.controller.php: 最后是riff.fare.controller.php:
public static function getFare($int_terminate,$fieldPickup) {
if($fieldPickup > 1100) {
$fare = 222.00;
}
else {
$fare = 111.00;
}
}
In the firebug console I can see that field_runin_p = 1200
在Firebug控制台中,我可以看到
field_runin_p = 1200
However, if I do a var_dump($fieldPickup); 但是,如果我执行var_dump($ fieldPickup); this is NULL when it should be
1200
. 当它应该为
1200
时为NULL。 Any ideas why this is not working? 任何想法为什么这不起作用?
Any help would be much appreciated! 任何帮助将非常感激!
Your function requires two arguments: 您的函数需要两个参数:
getFare($int_terminate, $fieldPickup)
Your code however, passes only one (and it doesn't match the var you are expecting to be in): 但是,您的代码仅传递一个(并且与您期望的var不匹配):
echo TariffFareController::getFare($_REQUEST['field_runin_p']);
You are trying to send html from your PHP - header('Content-type: text/html');
您正在尝试从PHP发送HTML-
header('Content-type: text/html');
but you are telling your javascript to expect plaintext - dataType: 'text',
但是您告诉您的JavaScript需要纯文本
dataType: 'text',
Use header('Content-type: text/plain');
使用
header('Content-type: text/plain');
instead. 代替。
The other posters point out other problems with your script. 其他海报指出了脚本的其他问题。
I'd also advise against using $_REQUEST
as your data could be coming from GET or POST parameters, and you wouldn't know which. 我也建议不要使用
$_REQUEST
因为您的数据可能来自GET或POST参数,并且您不知道哪个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.