1

我有调用 javascript 函数的 php 代码:

onclick='fightit(0,0,0,0,0)'

这是javascript函数:

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});
}

当我调用函数并显示参数时,我得到了;

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

有什么想法我哪里出错了吗?

4

3 回答 3

1

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,请尝试这样的参数(您可能必须更改函数变量名称):

new Ajax.Updater('div01', 'php/fight.php', {method: 'get', parameters: {Nbr: nbr, Strt: strt, Bar: bar, FightType: type, Action: action}, onComplete: div05F})
于 2012-04-09T15:25:17.213 回答
0

试试这个:

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};
于 2012-04-09T15:46:35.743 回答
0

尝试使用 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}
    });
}
于 2012-04-10T12:01:15.237 回答