duanqian3953 2014-07-04 06:56
浏览 45
已采纳

Javascript没有替换PHP脚本中的InnerHTML

I have been working on a Garry's Mod loading screen recently and basically I was attempting to 'simplify' the map name by replacing it with a phrase. However, it appears PHP isn't printing the array value.

$map = $_GET["map"];
$map_list = array (
    "gm_construct" => "Construct",
    "gm_flatgrass" => "Flatgrass"
);
if (in_array($map, $map_list) == true)
{
    if ($map == $map_list[1])
    {
        print("<script>document.getElementById('map_name').innerHTML = '" . print($map_list['gm_construct']) . "'</script>");
    }
    else if ($map == $map_list[2])
    {
        print("<script>document.getElementById('map_name').innerHTML = '" . print($map_list['gm_flatgrass']) . "'</script>");
    }
}
  • 写回答

3条回答 默认 最新

  • duanchui1251 2014-07-04 07:08
    关注

    A couple issues with how you're accessing the associative array $map_list. Use the following:

    <?php
    $map = "Flatgrass"; // $_GET["map"];
    $map_list = array (
        "gm_construct" => "Construct",
        "gm_flatgrass" => "Flatgrass"
    );
    $key = array_search($map, $map_list);
    if ($key !== false) {
        print("<script>document.getElementById('map_name').innerHTML = '" . $map_list[$key] . "'</script>");
    }
    

    If the $_GET value has a key in the $map_list, just use it directly to print the script.

    Don't call print() inside a concatenated string (it outputs directly to standard out, doesn't return a string). And, this reduces your code, by just having one if statement print out the script using the value passed in directly.

    And, in your original code, you can't index $map_list using integers, as you have only used string keys making it an associative array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?