开发者

getElementId returns null or undefined

I'm using PHP, AJAX with jQuery so when this if statment process it will show all the players that have been selected along with placing a remove button and a onclick="removeplayer(test);". But when I click the "Remove player" button I will either get a null or undefined.

PHP

   if(count($_SESSION['players'],COUNT_RECURSIVE) == $_SESSION['savedPlayers'] )
{

    foreach($_SESSION['players'] as $key => $value){
    echo "Player #:  . $key . ; Player Name: . $value . \n";
    echo "<input type=\"button\" name=\"" . $key . " \" id=\"" .  $value . "  \" value=\"Remove\" onclick=\"removePlayer(" . $value . ");\"> <br />";
    }

    echo "Max number of Players";
}

And my JavaScript is basic now because I'm just trying to make sure I'm getting the id passed correctly. I eventually will add jQuery that will remove the player from the list.

Here is my JavaScript:

function removePlayer(test){    

  开发者_运维技巧  alert(test);
    };


Its because that you have not enclosed the $value within quotes try this

echo "<input type=\"button\" name=\"" . $key . " \" id=\"" .  $value . "  \" value=\"Remove\" onclick=\"removePlayer('" . $value . "');\"> <br />";


You're generating malformed Javascript - by forgetting to surround your onclick function call with quotes. Given you've got a seriously terminal case of "leaning toothpick syndrome" in your code, try outputting the HTML like this:

    $safe_value = json_encode($value);
    echo <<<EOL
Player #: $key  Player Name: $value
<input type="button" name="$key" id="$value" value="Remove" onclick="removePlayer($safe_value);"><br />
EOL;

Note the use of json-encode to ensure that the $value is actual proper JS data. Using HEREDOCs for this is deal, as it eliminate any need to escape any quotes in the text you're outputting, plus allows direct insertion of variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜