【问题标题】:PHP array : Echo a $var inside a form with option value [closed]PHP数组:在带有选项值的表单内回显$ var [关闭]
【发布时间】:2014-12-02 05:03:34
【问题描述】:

如何在表单选项值中显示变量 $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>**
    }
?>

【问题讨论】:

  • 什么是.j son 文件?另外,“不工作”是什么意思?你有任何错误吗?如果没有,那么您是否关闭了错误?
  • **echo 这会抛出错误
  • @CodingAnt OP 试图突出显示该行。
  • 你为什么有**?这肯定会引发错误。接下来我看到您将 $json->goali 分配给 $goalies。是那个数组吗?如果它的数组,你不能用 $player->name 访问变量,但是 $player['name']。当然,您需要在此处显示的 php 代码之前有
  • 哦,我们怎么知道,OP 本来可以用评论来突出显示这个@Ja͢ck

标签: php arrays forms


【解决方案1】:

语句末尾没有双引号和分号。

 <?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>";
}
?>

【讨论】:

  • 如果你要修复他的代码,那么你至少应该进行一些错误检查。
  • true,至少没有显示错误 - 我仍在尝试显示带有多个 var 的选项值菜单。我需要添加表单标签吗?谢谢
  • 作为问题,这段代码是一个等待发生的 XSS。
【解决方案2】:

foreach 中的 $json-&gt;goalie 值是什么,请改用 $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>";
}
?>

【讨论】:

  • 如果玩家名称中有撇号会怎样?
【解决方案3】:

除了您的代码中存在明显的解析错误之外,还有其他一些需要注意的事项:

$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')
    );

【讨论】:

    【解决方案4】:
    1. 我建议您将 JSON 解码为数组,而不是对象,这样更易​​于使用。
    2. 您没有在上面的代码中使用goalies 变量,我假设这就是您想在foreach 语句中使用的变量?
    3. 错误检查是个好主意

    如果是这样,那么您的代码可能如下所示:

    <?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】:

      如果你想在字符串中显示变量,那么总是使用 "" 来显示变量。您可以将代码修改为:

      echo "<option value='{$player->name}'>{$player->name}</option>";
      

      所以永远记住: 1.可以在""(双引号)中使用''(单引号)

      【讨论】:

        【解决方案6】:

        您没有在变量周围正确使用'

        Demo

        <?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>';
        

        这会打印出以下内容。

        &lt;option value=" Occam's Razor "&gt;Occam's Razor&lt;/option&gt;

        【讨论】:

          【解决方案7】:

          与其写问题是什么,我认为只是向您展示并给您一些指示会更有帮助:

          // 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>";
          }
          

          【讨论】:

          • 输出转义到哪里了?
          • @Ja͢ck 我没有触及最后几行。问题在于不检查错误。输出的安全性是另一个时间。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多