Reputation: 13
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);
};
Upvotes: 1
Views: 106
Reputation: 360782
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.
Upvotes: 1
Reputation: 4995
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 />";
Upvotes: 3