dpp78272 2012-04-09 15:07
浏览 53
已采纳

PHP,Javascript,Ajax传递参数

I have php code that calls a javascript function:

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

Here is the 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});
}

When I call the function and display params I get;

Nbr=1&Strt=0&Bar=0&FightType=0&Action=0

This is what I'm suppose to get but when I use it in my php:

if (!isset($_GET['Action'])) {
    $_GET['Action'] = 0;
}
if (isset($_GET['FightType'])) {
   $fighttype = $_GET['FightType'];
}
else {
   $fighttype = 0; 
}
$s = $_GET['FightType'];

Action is set but FightType is not when I execute this line of code:

$s = $_GET['FightType'];

I get:

Undefined index: FightType in C:\wamp\www\hand\php\div08F.php on line 10

Any ideas where I'm going wrong?

  • 写回答

3条回答 默认 最新

  • doujiaozhan2413 2012-04-09 15:25
    关注

    EDIT2: OK, with that information, I tested out. I think you are using one file, so I set up a mock php file to test things. I removed the onComplete and set a div with the update. Here is the result that works. Let me know if it helps:

    <?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>
    

    ORIGINAL:

    You are getting the fighttype error, because even though you check for it, you still use it after the check without rechecking ($_GET['FightType'] still doesn't exist). Try this:

    if (isset($_GET['FightType'])) {
       $fighttype = $_GET['FightType'];
    }
    else {
       $fighttype = 0; 
    }
    $s = $fighttype;
    

    EDIT: to fix the ajax, try parameters like this (you might have to change the function variable names):

    new Ajax.Updater('div01', 'php/fight.php', {method: 'get', parameters: {Nbr: nbr, Strt: strt, Bar: bar, FightType: type, Action: action}, onComplete: div05F})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加