【问题标题】:How do i change page elements with php?如何使用 php 更改页面元素?
【发布时间】:2018-02-27 11:49:43
【问题描述】:

我想使用 GET 变量和开关更改图像,但我不知道如何更改图像。这是我的 html/php。

<img name="foo" src="bar.gif" alt="foobar">
<?php
switch($_GET['ine']) {
case "foo"
> I dont know what to put here.
break;


case "bar"
> I dont know what to put here.
break;
}

?>

【问题讨论】:

  • 只需echo您要显示的图像。另外,不要忘记为您的 switch 语句实现 default 案例。
  • $_GET['ine'] 包含什么?路径、图像标签还是只是名称?

标签: php html image


【解决方案1】:

你可以这样做:

 <?php
    switch($_GET['ine']) {
    case "foo":
     echo'<img name="foo" src="bar.gif" alt="foobar">';
    break;


    case "bar":
    echo '<img name="foo" src="bar_2.gif" alt="foobar">'; // Another image
    break;

    default:
     echo "Something you want to do when no $_GET[] case matches;
    break;

    }

    ?>

只需在您想要的情况下输出图像,因此您可以为每种情况更改图像的来源。另外,添加一个默认情况。真的很重要,否则你会得到一些错误。

【讨论】:

    【解决方案2】:

    写完后不能用php动态改变,但可以这样:

    <?php
    switch($_GET['ine']) {
    case "foo"
    echo '<img name="foo" src="bar1.gif" alt="foobar">';
    break;
    
    
    case "bar"
    echo '<img name="foo" src="bar2.gif" alt="foobar">';
    break;
    }
    
    ?>
    

    这样$_GET['ine'] 决定输出两者中的哪一个

    如果你想让它更有活力,你可以去那里做这个:

    echo '<img name="foo" src="'.$_GET['ine'].'.gif" alt="foobar">';
    

    但我建议您先阅读输入清理

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 2010-09-12
      • 2016-02-15
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多