如何在表单选项值中显示变量$player->name,以便用户可以选择变量并提交表单。
这是我的代码不起作用:
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.j son");
$json = json_decode($result);
$goalies = $json->goali;
foreach ($json->goalie as $player) {
**echo "<option value='"".$player->name."'">".$player->name."</option>**
}
?>
语句末尾没有双引号和分号。
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
$json = json_decode($result);
$goalies = $json->goali;
foreach ($json->goalie as $player) {
echo "<option value='"".$player->name."'">".$player->name."</option>";
}
?>
这个$json->goalie
值是多少 foreach
,改用$goalies
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
$json = json_decode($result);
$goalies = $json->goali;
foreach ($goalies as $player)
{
echo "<option value='".$player->name."'>".$player->name."</option>";
}
?>
除了代码中明显的解析错误外,还需要注意以下一些其他事项:
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
你不能只在这样的URL中使用$team
,你应该对它进行编码:
$url = sprintf("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/%s/iphone/clubroster.json",
urlencode($team)
);
$result = file_get_contents($url);
$json = json_decode($result);
foreach ($json->goalie as $player) {
echo "<option value='"".$player->name."'">".$player->name."</option>";
}
您应该始终在输出中转义变量:
printf('<option value="%s">%1$s</option>',
htmlspecialchars($player->name, ENT_QUOTES, 'UTF-8')
);
- 我建议将JSON解码为数组,而不是对象,这样更容易使用。
- 您没有在上面的代码中使用
goalies
变量,我假设这就是您想在foreach
语句中使用的变量? - 错误检查将是一个好主意
如果是这样,那么您的代码可能如下所示:
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
$json = json_decode($result, true);
$goalies = array();
if (!empty($json['goali'])) {
$goalies = $json['goali'];
}
foreach ($goalies as $player) {
echo '<option value="' . $player['name'] . '">' . $player['name'] . '</option>'
}
?>
如果要
div class="answers"> 在字符串中显示变量,请始终使用"来显示变量。您可以按以下方式修改代码:
echo "<option value='{$player->name}'>{$player->name}</option>";
所以永远记住:1. 您可以在"(双引号)中使用"(单引号)"<</p>
您没有在变量周围正确使用'
。
演示
<?php
// Create a dummy Object
$player = new stdClass();
// Create a dummy property
$player->name = "Occam's Razor";
// Print it out
echo '<option value=" '.htmlspecialchars($player->name).' ">' .htmlspecialchars($player->name). '</option>';
这将打印以下内容。
<option value=" Occam's Razor ">Occam's Razor</option>
与其写问题是什么,我认为只是向您展示并为您提供一些指示会更有帮助:
// Make sure all errors are displayed
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Get the team variable and make sure that it was set
$team = filter_input(INPUT_POST, 'team', FILTER_UNSAFE_RAW);
if ($team === null) {
die('Error: The "team" POST variable is not set, cannot continue');
}
// Since you are using the variable inside a URL it makes
// sense to remove potentially unsafe characters; or in
// other words, only preserve characters that are allowed.
// Encoding the variable, as in Ja͢ck's answer, is also an option.
$team = preg_replace('/[^a-zA-Z0-9_-]/', null, $team);
if ($team === "") {
die('Error: The "team" variable is empty, cannot continue');
}
// Construct the URL
$url = "http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/{$team}/iphone/clubroster.json";
// Grab the data from the website
$data = file_get_contents($url);
if ($data === false) {
die('Error: Could not grab the data from the URL, cannot continue');
}
// Attempt to decode the data
$json = json_decode($data);
if (json_last_error() !== JSON_ERROR_NONE) {
die('Error: There was a JSON error ('.json_last_error_msg().'), cannot continue');
}
// Since we are expecting an object we should check for it
// before using it
if ( ! is_object($json)) {
die('Error: The JSON was decoded, but is not an object, cannot use it...');
}
// If the following code gives you problems then read the error
// message, try to understand what it says, and then google it if
// you are getting nowhere
$goalies = $json->goali;
foreach ($json->goalie as $player) {
echo "<option value='"".$player->name."'">".$player->name."</option>";
}