GET / POST for the same page?

0
Hello, I'm a beginner in PHP and I'm following some videos-classes, in one we pass parameters via GET or POST (whatever) to the same form page, the goal is only to work with radiobox and checkbox , however I have two problems, first I'll put the page code:

<?php
    $escolha = $_GET['cor'];
    $termos = $_GET['concorda'];
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Produtos</title>

<style> 
    #azul{color: blue;}
    #verde{color: green;}
    #vermelho{color: red;}
</style>
</head>
<body>

<form method="get" action="if_else.php">
        <table>  
        <tr>
             <td><h1>Escolha uma cor:</h1></td>
        </tr>
        <tr>
            <td>
                <p id="azul"><input type="radio" name="cor" value="0">Azul</p>
                <p id="verde"><input type="radio" name="cor" value="1">Verde</p>
                <p id="vermelho"><input type="radio" name="cor" value="2">Vermelho</p>
            </td>
            <?php 
            if($escolha == 0)
            {
                echo '<td bgcolor="blue"></td>';
            }
            else if($escolha == 1)
            {
                echo '<td bgcolor="green"></td>';
            }
            else
            {
                echo '<td bgcolor="red"></td>';
            }
            ?>
        </tr>
        <tr>
            <td><input type="checkbox" name="concorda"/> Concordo com tudo.</td>
        </tr>
        <tr>
            <td>
                <?php
                if($termos)
                {
                    echo "Concordor com os termos!";
                }
                else
                {
                    echo "Não concordou.";
                }
                ?>
            </td>
            <td><input type="submit" value="Pronto"/></td>
        </tr>
        </table>
</form>
</body>
</html>

Sorry for the bad formatting, etc., it is just for the purpose of practicing php.

Problem 01:

The first time I access the page I do not have values for the variables escolha and termos , so apache points to error, how does it vary to solve this?

Problem 02:

When you do not set checkbox and tighten on submit the variable termos does not receive anything and apache points error again.

Where am I going wrong? Thank you in advance.

    
asked by anonymous 15.12.2015 / 21:21

1 answer

1

Well, with the @rray link it looks like this:

<?php
    if(isset($_GET['cor']))
       $escolha = $_GET['cor'];
    else
       $escolha = 0;
    if(isset($_GET['concorda']))
        $termos = $_GET['concorda'];
    else
       $termos = "off";

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Produtos</title>

<style> 
    #azul{color: blue;}
    #verde{color: green;}
    #vermelho{color: red;}
</style>
</head>
<body>

<form method="get" action="#">
        <table>  
        <tr>
             <td><h1>Escolha uma cor:</h1></td>
        </tr>
        <tr>
            <td>
                <p id="azul"><input type="radio" name="cor" value="0">Azul</p>
                <p id="verde"><input type="radio" name="cor" value="1">Verde</p>
                <p id="vermelho"><input type="radio" name="cor" value="2">Vermelho</p>
            </td>
            <?php 
            if($escolha == 0)
            {
                echo '<td bgcolor="blue"></td>';
            }
            else if($escolha == 1)
            {
                echo '<td bgcolor="green"></td>';
            }
            else
            {
                echo '<td bgcolor="red"></td>';
            }
            ?>
        </tr>
        <tr>
            <td><input type="checkbox" name="concorda"/> Concordo com tudo.</td>
        </tr>
        <tr>
            <td>
                <?php
                if($termos == 'on')
                {
                    echo "Concordo com os termos!";
                }
                else
                {
                    echo "Não concordou.";
                }
                ?>
            </td>
            <td><input type="submit" value="Pronto"/></td>
        </tr>
        </table>
</form>
</body>
</html>

As you can see, I changed the receipt of the variables, to receive GET or POST only when it has a passed value, and if it does not put 0 and off in variables. Finally, I also changed the condition of the checkbox, now checking to see if it is on , since before I was just checking to see if it exists.

    
15.12.2015 / 21:38