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.
How to display the result of a form on the same page?
– rray
Thanks, looking there I managed to do, I will post as it turned out.
– Leonardo
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?
– Leonardo
You can use javascript to do this if you need some php information use ajax.
– rray