GET/POST to the same page?

Asked

Viewed 5,068 times

0

Hello, I’m a beginner in PHP and I’m following some videos-lessons, in a we passed parameters via GET or POST (whatever it takes) to the same page of the form, the goal is just to work with radiobox and checkbox, but I’m having 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 have no values for the variables escolha and termos, soon the apache points error, as varies to solve this?

Problem 02:

When I don’t mark the checkbox and tightening in the submit the variable termos receives nothing and the apache points error again.

Where am I wrong? I thank you in advance.

  • 5
  • Thanks, looking there I managed to do, I will post as it turned out.

  • In case, to complement, would it be possible for me to do this in real time? That is, when the user presses the radio he changes the color without having to press the button?

  • 1

    You can use javascript to do this if you need some php information use ajax.

1 answer

1


Well, with the link @rray was 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 altered the receipt of the variables to receive from GET or POST only when it has a past value, and if it has not put 0 and off variables. Finally, I also changed the condition of the checkbox, now checking if it is on, because before I was just checking if it exists.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.