php-在我选择一个选项之前切换失败

发布于 2025-01-10 13:56:10 字数 2232 浏览 3 评论 0原文

我在使用 php 切换功能时遇到问题。我知道它一定很简单,但我只是在学习,我似乎找不到正确的方法来编码它。 这就是问题所在:当您单击任何选项时它工作正常,但是当我第一次加载页面时它会发送错误消息...

<body>
    <?php include ('./header.php')?>

    <section>
        <div class="btn">
            <ul>
                <li><a href="./index.php?opcion=op1">Opcion 1</a></li>
                <li><a href="./index.php?opcion=op2">Opcion 2</a></li>
                <li><a href="./index.php?opcion=op3">Opcion 3</a></li>
            </ul>
        </div>

        <?php 
            if(isset ($_GET['opcion'])) {

            
            switch($_GET['opcion']) {
                case 'op1' :
                    $nombre = 'Magnus Carlsen';
                    $posicion = '1';
                    $puntaje = '2840';
                    $img = './magnus.jpg';
                break;
                case 'op2' :
                    $nombre = 'Ian Nepomniatchi';
                    $posicion = '2';
                    $puntaje = '2812';
                    $img = './nepo.jpeg';
                break;
                case 'op3' :
                    $nombre = 'Ding Liren';
                    $posicion = '3';
                    $puntaje = '2760';
                    $img = './ding.jpg';
                break;
                }
            }
            else {
                
            }
            
            ?>
             <div class="show">
                    <h3><?php echo $nombre?></h2>
                    <h4><?php echo $posicion?></h4>
                    <p><?php echo $puntaje?></p>
                    <div class="contenedor_img">
                        <img src="<?php echo $img ?>" alt="foto">
                    </div>
                </div>

现在,我认为“show”div 不应该在那里...但是当我移动它时它也没有很好地响应...

这是错误:

警告:未定义的变量 $nombre in C:\xampp\htdocs\dashboard\proyectos\PHP\index.php on line 53

警告:未定义的变量$位置在第 54 行上的 C:\xampp\htdocs\dashboard\proyectos\PHP\index.php

警告:第 55 行上的 C:\xampp\htdocs\dashboard\proyectos\PHP\index.php 中未定义变量 $puntaje 照片

I'm having a problem with php switch function. I know it must be simple, but i'm just learning and I can't seem to find the right way to code it.
This is the issue: It works fine when you click any of the options, but it sends an error msg when I first load up the page...

<body>
    <?php include ('./header.php')?>

    <section>
        <div class="btn">
            <ul>
                <li><a href="./index.php?opcion=op1">Opcion 1</a></li>
                <li><a href="./index.php?opcion=op2">Opcion 2</a></li>
                <li><a href="./index.php?opcion=op3">Opcion 3</a></li>
            </ul>
        </div>

        <?php 
            if(isset ($_GET['opcion'])) {

            
            switch($_GET['opcion']) {
                case 'op1' :
                    $nombre = 'Magnus Carlsen';
                    $posicion = '1';
                    $puntaje = '2840';
                    $img = './magnus.jpg';
                break;
                case 'op2' :
                    $nombre = 'Ian Nepomniatchi';
                    $posicion = '2';
                    $puntaje = '2812';
                    $img = './nepo.jpeg';
                break;
                case 'op3' :
                    $nombre = 'Ding Liren';
                    $posicion = '3';
                    $puntaje = '2760';
                    $img = './ding.jpg';
                break;
                }
            }
            else {
                
            }
            
            ?>
             <div class="show">
                    <h3><?php echo $nombre?></h2>
                    <h4><?php echo $posicion?></h4>
                    <p><?php echo $puntaje?></p>
                    <div class="contenedor_img">
                        <img src="<?php echo $img ?>" alt="foto">
                    </div>
                </div>

Now, I suppose the "show" div shouldn't be there... but it doesn't responde well when i move it around either...

this are the errors:

Warning: Undefined variable $nombre in C:\xampp\htdocs\dashboard\proyectos\PHP\index.php on line 53

Warning: Undefined variable $posicion in C:\xampp\htdocs\dashboard\proyectos\PHP\index.php on line 54

Warning: Undefined variable $puntaje in C:\xampp\htdocs\dashboard\proyectos\PHP\index.php on line 55
foto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2025-01-17 13:56:10

问题是,当表单未提交时,您不会输入开关。这意味着您的 $nombre$posicion$puntaje 以及很可能的 $img 尚未定义。有两种方法可以解锁自己。

选项 1,为这些变量指定默认值

// here you can define your variables
$nombre = 'no number';
$posicion = 'no position';
$puntaje = 'empty';
$img = '';
if (isset ($_GET['opcion'])) {
    switch ($_GET['opcion']) {
        case 'op1' :
            $nombre = 'Magnus Carlsen';
            $posicion = '1';
            $puntaje = '2840';
            $img = './magnus.jpg';
            break;
        case 'op2' :
            $nombre = 'Ian Nepomniatchi';
            $posicion = '2';
            $puntaje = '2812';
            $img = './nepo.jpeg';
            break;
        case 'op3' :
            $nombre = 'Ding Liren';
            $posicion = '3';
            $puntaje = '2760';
            $img = './ding.jpg';
            break;
    }
} else {
}

,或者您可以检查用户是否已提交表单,然后才尝试回显变量。您可以通过将 html 包含在 if 语句中来完成此操作。

<?php if(isset ($_GET['opcion'])) { ?>
<div class="show">
    <h3><?php echo $nombre?></h2>
        <h4><?php echo $posicion?></h4>
        <p><?php echo $puntaje?></p>
        <div class="contenedor_img">
            <img src="<?php echo $img ?>" alt="foto">
        </div>
</div>
<?php } ?>

The problem is that when the form is not submitted, you do not enter your switch. that means your $nombre, $posicion, $puntaje and most probably $img are not defined. There are 2 ways to unblock yourself.

Option 1, give a default value to these variables

// here you can define your variables
$nombre = 'no number';
$posicion = 'no position';
$puntaje = 'empty';
$img = '';
if (isset ($_GET['opcion'])) {
    switch ($_GET['opcion']) {
        case 'op1' :
            $nombre = 'Magnus Carlsen';
            $posicion = '1';
            $puntaje = '2840';
            $img = './magnus.jpg';
            break;
        case 'op2' :
            $nombre = 'Ian Nepomniatchi';
            $posicion = '2';
            $puntaje = '2812';
            $img = './nepo.jpeg';
            break;
        case 'op3' :
            $nombre = 'Ding Liren';
            $posicion = '3';
            $puntaje = '2760';
            $img = './ding.jpg';
            break;
    }
} else {
}

or you can check to see if user has submitted the form and only then try to echo the variables. You can do this by surrounding the html in an if statement.

<?php if(isset ($_GET['opcion'])) { ?>
<div class="show">
    <h3><?php echo $nombre?></h2>
        <h4><?php echo $posicion?></h4>
        <p><?php echo $puntaje?></p>
        <div class="contenedor_img">
            <img src="<?php echo $img ?>" alt="foto">
        </div>
</div>
<?php } ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文