i try to build a formulaire that contain a text input and a button when i push the button i want to display some messages in my code below
Example
if my first character is an A
i want to display "GROUP1-1"
else if my first character is an B
i want to display "GROUP1-2"
else if my first character is an C
i want to display "GROUP1-3"
if my second character is an F
i want to display "GROUP2-1"
else if my my second character is an G
i want to display "GROUP2-2"
this is my code when i push the button the program work but always there are this message "Notice: Undefined index: t in C:\Program Files (x86)\EasyPHP 2.0b1\www\exercice\test1\test1.php on line 18,22,26,30,34"
and the program work
i use Notepad++ and easyPHP
<html>
<head>
<meta charset="utf-8"/>
<title>
exercice
</title>
</head>
<body>
<form action="" method="post">
decoder ces caracteres
<input type = "text" name ="t" size="17"/>
<br>
<br>
<center>
<input type ="submit" name ="decoder" value="Decoder">
</center>
<?php
if($_GET['t'][0]=='A') {
echo "GROUP1-1";
}
else
if($_GET['t'][0]=='B') {
echo "GROUP1-2";
}
else
if($_GET['t'][0]=='C') {
echo "GROUP1-3";
}
if($_GET['t'][1]=='F') {
echo "GROUP2-1";
}
else
if($_GET['t'][1]=='G') {
echo "GROUP2-2";
}
?>
</form>
</body>
</html>
This is what I mean, You should also do a test to know if the form was submitted
if(isset($_POST['t']))
{
if($_POST['t'][0]=='A')
echo"GROUP1-1";
}
Your form action is set to post
but you're using $_GET
. Change $_GET
to $_POST
.
<?php
if(!empty($_POST['t']) && $_POST['t']=='A') {
echo "GROUP1-1";
} else if(!empty($_POST['t']) && $_POST['t']=='B') {
echo "GROUP1-2";
} else if(!empty($_POST['t']) && $_POST['t']=='C') {
echo "GROUP1-3";
} else if(!empty($_POST['t']) && $_POST['t']=='F') {
echo "GROUP2-1";
} else if(!empty($_POST['t']) && $_POST['t']=='G') {
echo "GROUP2-2";
}
?>
Or a better solution would be to use switch
:
if(!empty($_POST['t'])) {
switch($_POST['t']) {
case 'A':
echo "GROUP1-1";
break;
case 'B':
echo "GROUP1-2";
break;
case 'C':
echo "GROUP1-3";
break;
case 'F':
echo "GROUP2-1";
break;
case 'G':
echo "GROUP2-2";
break;
default:
echo "Invalid Input";
break;
}
}