Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP、Javascript、Ajax传递参数_Php_Javascript_Ajax - Fatal编程技术网

PHP、Javascript、Ajax传递参数

PHP、Javascript、Ajax传递参数,php,javascript,ajax,Php,Javascript,Ajax,我有调用javascript函数的php代码: onclick='fightit(0,0,0,0,0)' function fightit(nbr,strt,bar,type,action) { var params = "Nbr="+nbr; params += "&Strt="+strt; params += "&Bar="+bar; params += "&FightType="+type; params += "&Action="+action; ale

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

onclick='fightit(0,0,0,0,0)'
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});
}
下面是javascript函数:

onclick='fightit(0,0,0,0,0)'
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

知道我哪里出错了吗

编辑2:好的,有了这些信息,我考完了。我认为您使用的是一个文件,所以我设置了一个模拟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>
编辑:要修复ajax,请尝试以下参数(可能需要更改函数变量名):


编辑2:好的,有了这些信息,我测试了一下。我认为您使用的是一个文件,所以我设置了一个模拟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>
编辑:要修复ajax,请尝试以下参数(可能需要更改函数变量名):

请尝试以下方法:

function fightit(nbr,strt,bar,type,action) {
    var params = "{Nbr:" + nbr + ",Strt:" + strt + ",Bar:" + bar + "FightType:" + type + ",Action:" + action}";
    etc...
编辑:好的,这基本上与craniumonempty的建议相同,它是在我之前发布的

编辑:为了完整起见,并回应craniumonempty的评论:这可能应该是(而且更简单):

请尝试以下方法:

function fightit(nbr,strt,bar,type,action) {
    var params = "{Nbr:" + nbr + ",Strt:" + strt + ",Bar:" + bar + "FightType:" + type + ",Action:" + action}";
    etc...
编辑:好的,这基本上与craniumonempty的建议相同,它是在我之前发布的

编辑:为了完整起见,并回应craniumonempty的评论:这可能应该是(而且更简单):


尝试使用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}
    });
}

尝试使用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}
    });
}


PHP不能直接调用javascript方法,因为PHP在服务器端运行,javascript在客户端运行。为什么要将
$\u GET['FightType']
的值设置为两个变量(
$s
$FightType
)?是否打印($\u GET)显示您期望的内容?您是否碰巧尝试在PHP脚本的开头打印\u r或var\u dump$\u GET以确保它都在那里?我在if外部将其设置为$s只是为了查看我得到的内容,这是我得到错误的行。在PHP开始时,我在$\u GET['Action']上做了一个var_转储,效果很好,但在$\u GET['FightType']上做时,我得到了一个未定义的索引。我不是直接从PHP调用javascript,这是对我设置的按钮的onClick事件的响应。UPPPP不能直接调用javascript方法,因为PHP在服务器端运行,javascript在客户端运行。为什么要将
$\u GET['FightType']
的值设置为两个变量(
$s
$FightType
)?print_r($_GET)是否显示您期望的内容?您是否碰巧尝试在PHP脚本的开头打印\u r或var_dump$_GET,以确保所有内容都在那里?我在if之外将其设置为$s只是为了查看我得到的内容,这是我得到错误的行。在PHP开始时,我在$\u GET['Action']上做了一个var_转储,效果很好,但在$\u GET['FightType']上做时,我得到了一个未定义的索引。我不是直接从PHP调用javascript,它是对我设置的按钮的onClick事件的响应,我不了解?。我的程序有大量的Ajax调用来传递参数,这是它第一次不起作用。最大的问题是为什么$u GET['FightType']没有设置。@S.e.Estes是的,刚刚注意到了这一点。能否将_get数组(和/或查询字符串)作为字符串传回javascript并输出以查看发生了什么?@S.e.Estes检查编辑。。。这就是他们在这里设置参数的方式:我从来没有在其他ajax调用中设置过这样的参数。为什么在这一个实例中我必须做不同的事情呢?我改变了我的调用,使之看起来像这样:new Ajax.Updater('div01','php/fight.php',{method'get',参数:{Nbr:Nbr,Strt:Strt,Bar:Bar,FightType:type,Action:Action},onComplete:div05F});但仍然是战斗型的,而不是行动型的。为什么其他参数传递正确,而不是那个参数?我不明白这个?。我的程序有大量的Ajax调用来传递参数,这是它第一次不起作用。最大的问题是为什么$u GET['FightType']没有设置。@S.e.Estes是的,刚刚注意到了这一点。能否将_get数组(和/或查询字符串)作为字符串传回javascript并输出以查看发生了什么?@S.e.Estes检查编辑。。。这就是他们在这里设置参数的方式:我从来没有在其他ajax调用中设置过这样的参数。为什么在这一个实例中我必须做不同的事情呢?我改变了我的调用,使之看起来像这样:new Ajax.Updater('div01','php/fight.php',{method'get',参数:{Nbr:Nbr,Strt:Strt,Bar:Bar,FightType:type,Action:Action},onComplete:div05F});但仍然是战斗型的,而不是行动型的。为什么其他参数传递正确,而不是那个参数?它会这样工作吗?我想它应该是这样的:
var-params={Nbr:Nbr,Strt:Strt,Bar:Bar,FightType:type,Action:Action}
@craniumonempty:我想你是对的,它可能不应该是字符串,它应该是一个关联数组。这也不行。警报似乎显示参数正确传递though@S.e.Estes:只是为了检查,将“get”替换为“post”,看看这是否有区别?我对所有正在传递的参数都尝试了var_dump,但没有一个参数传递正确,只是巧合,设置了操作,这样行吗?我想它应该是这样的:
var-params={Nbr:Nbr,Strt:Strt,Bar:Bar,FightType:type,Action:Action}
@craniumonempty:我想你是对的,它可能不应该是字符串,它应该是一个关联数组。这也不行。警报似乎显示参数正确传递though@S.e.Estes:只是为了检查,将“get”替换为“post”,看看是否