[英]PHP, Javascript, Ajax passing parameters
我有 php 代码调用 javascript function:
onclick='fightit(0,0,0,0,0)'
这是 javascript function:
function fightit(nbr,strt,bar,type,action) {
var params = "Nbr="+nbr;
params += "&Strt="+strt;
params += "&Bar="+bar;
params += "&FightType="+type;
params += "&Action="+action;
alert(params);
new Ajax.Updater('div01', 'php/fight.php', {method: 'get', parameters: params, onComplete: div05F});
}
当我拨打 function 并显示我得到的参数时;
Nbr=1&Strt=0&Bar=0&FightType=0&Action=0
这是我应该得到的,但是当我在我的 php 中使用它时:
if (!isset($_GET['Action'])) {
$_GET['Action'] = 0;
}
if (isset($_GET['FightType'])) {
$fighttype = $_GET['FightType'];
}
else {
$fighttype = 0;
}
$s = $_GET['FightType'];
当我执行这行代码时,动作已设置但 FightType 未设置:
$s = $_GET['FightType'];
我得到:
Undefined index: FightType in C:\wamp\www\hand\php\div08F.php on line 10
我哪里出错了?
EDIT2:好的,有了这些信息,我进行了测试。 我想你正在使用一个文件,所以我设置了一个模拟 php 文件来测试东西。 我删除了 onComplete 并使用更新设置了一个 div。 这是有效的结果。 让我知道是否有帮助:
<?php
if ( isset($_GET['Nbr']) ){
// here Nbr is set, so we drop into outputting xml... you can do more before this
// and you can open a separate file, but didn't know how things were set up for you.
header('Content-Type: text/xml');
$out = $_GET['Nbr'].','
.(isset($_GET['Strt'])?$_GET['Strt']:'').','
.(isset($_GET['Bar'])?$_GET['Bar']:'').','
.(isset($_GET['FightType'])?$_GET['FightType']:'').','
.(isset($_GET['Action'])?$_GET['Action']:'');
print '<?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'><span>'.htmlentities($out).'</span>';
exit();
}
?>
<html>
<head>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
function fightit(nbr,strt,bar,type,action) {
var params = "Nbr=" + nbr
+ "&Strt=" + strt
+ "&Bar=" + bar
+ "&FightType=" + type
+ "&Action=" + action;
alert(params);
// this is actually calling itself with '/t.php' and updating div01
new Ajax.Updater('div01', '/t.php', {method: 'get', parameters: params});
}
</script>
</head>
<body>
<table style="border-style:none;">
<tr>
<td style="border-style:none;">
<input style="width:150px; text-align:center;" type="button" value="Steal" onclick="stealit()" />
</td>
<td id="fightBtn" style="border-style:none;"><input style="width:150px; text-align:center;" type="button" value="Fight" onclick="fightit(0,0,0,0,0)" />
</td>
</tr>
<div id="div01"></div>
</body>
</html>
原来的:
您收到了 fighttype 错误,因为即使您检查了它,您仍然在检查后使用它而无需重新检查( $_GET['FightType']
仍然不存在)。 试试这个:
if (isset($_GET['FightType'])) {
$fighttype = $_GET['FightType'];
}
else {
$fighttype = 0;
}
$s = $fighttype;
编辑:要修复 ajax,请尝试这样的参数(您可能必须更改 function 变量名称):
new Ajax.Updater('div01', 'php/fight.php', {method: 'get', parameters: {Nbr: nbr, Strt: strt, Bar: bar, FightType: type, Action: action}, onComplete: div05F})
试试这个:
function fightit(nbr,strt,bar,type,action) {
var params = "{Nbr:" + nbr + ",Strt:" + strt + ",Bar:" + bar + "FightType:" + type + ",Action:" + action}";
etc...
编辑:好的,这与 craniumonempty 的建议基本相同,后者是在我之前发布的。
编辑:为了完整起见,并回应 craniumonempty 的评论:这应该是(更简单的):
var params = {Nbr:nbr, Strt:strt, Bar:bar, FightType:type, Action:action};
尝试使用 jQuery.ajax 而不是使用 Ajax 原型:
function fightit(nbr,strt,bar,type,action) {
jQuery.ajax({
url: 'php/fight.php',
type: 'GET',
data: {'Nbr': nbr, 'Strt': strt, 'Bar': bar, 'FightType': type, 'Action': action}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.