如何在表单选项值中显示变量$ 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>**
}
?>
答案 0 :(得分:2)
在声明结尾处没有双引号和分号。
<?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>";
}
?>
答案 1 :(得分:2)
$json->goalie
中的foreach
值是什么,请改用<{1}}
$goalies
答案 2 :(得分:1)
除了你的代码中有明显的解析错误之外,还有一些需要注意的事项:
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
你不能在这样的网址中使用$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')
);
答案 3 :(得分:0)
如果你想在字符串中显示变量,那么总是使用&#34;&#34;用于显示变量。您可以将代码修改为:
echo "<option value='{$player->name}'>{$player->name}</option>";
所以永远记住: 1.您可以使用&#39;(单曲)&#34;&#34;&#34;(双引号)
答案 4 :(得分:0)
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>'
}
?>
答案 5 :(得分:0)
您没有正确使用'
变量。
<?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>
答案 6 :(得分:0)
不是写下问题是什么,我认为向你展示更有帮助,并给你一些指示:
// 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>";
}