Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
3  
PHP can't call a javascript method directly, because PHP runs on server side and Javascript on client side. –  reporter Apr 9 '12 at 15:10
    
Why are you setting the value of $_GET['FightType'] to two variables ($s and $fighttype)? –  jrummell Apr 9 '12 at 15:11
2  
does print_r($_GET) display what you expect? –  David Nguyen Apr 9 '12 at 15:14
    
Did you by chance try to print_r or var_dump $_GET at the beginning of your PHP script to make sure it's all there? –  aupdo Apr 9 '12 at 15:16
    
I set it to $s outside the if just to see what I was getting, that is the line where I get the error. I did a var_dump on $_GET['Action'] at the beginning of the PHP and that worked fine but when I did it on $_GET['FightType'] I get an undefined index. I'm not callin the javascript directly from the PHP, it's a response to the onClick event of a button I set up –  S.e. Estes Apr 9 '12 at 15:22

3 Answers 3

up vote 1 down vote accepted

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})
share|improve this answer
    
I don't understand about the ?. My program has tons of Ajax calls that passes parameters and this is the first case where it doesn't work. The big question is why $_GET['FightType'] isn't set. –  S.e. Estes Apr 9 '12 at 15:28
    
@S.e.Estes yeah, just noticed that. Can you pass back the _get array (and/or query string) as a string to javascript and output to see what is going on? –  craniumonempty Apr 9 '12 at 15:30
    
Not sure I understand –  S.e. Estes Apr 9 '12 at 15:34
    
@S.e.Estes check the edit... that was how they set up parameters here: prototypejs.org/learn/introduction-to-ajax –  craniumonempty Apr 9 '12 at 15:40
    
I've never had to set up parameters like that in my other ajax calls. Why in this one instance would I have to do it differntly? –  S.e. Estes Apr 9 '12 at 15:50

Try this instead:

function fightit(nbr,strt,bar,type,action) {
    var params = "{Nbr:" + nbr + ",Strt:" + strt + ",Bar:" + bar + "FightType:" + type + ",Action:" + action}";
    etc...

EDIT: OK, this is essentially the same as craniumonempty's suggestion, which was posted before mine.

EDIT: For the sake of completeness, and in response to craniumonempty's comment: This should probably rather (and simpler) be:

var params = {Nbr:nbr, Strt:strt, Bar:bar, FightType:type, Action:action};
share|improve this answer
    
would it work that way? I thought it would have to be something like: var params = {Nbr: nbr, Strt: strt, Bar: bar, FightType: type,Action: action}; –  craniumonempty Apr 9 '12 at 15:49
    
@craniumonempty:I think you are right, it probably shouldn't be a string, it should be an associative array. –  Stefan Apr 9 '12 at 15:51
    
That doesn't work either. The alert seems to show the parameters passing correctly though –  S.e. Estes Apr 9 '12 at 16:00
    
@S.e.Estes: Just to check, replace 'get' with 'post' and see if that makes a difference? –  Stefan Apr 9 '12 at 17:11
    
Trying post didn't help –  S.e. Estes Apr 9 '12 at 17:36

Instead of using Ajax prototype, try to use jQuery.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}
    });
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.