Hi everyone,
A very simple code that aims to know whether value was inserted to an input field in a form is not working :-(.
In the following code, when a value is inserted to field 'A" I want ‘a’ to be echoed and if ‘b’ recieves value the program should
show ‘b’ on the display.
Whether I insert a value or weather no value is inserted at all the following program always shows shows both ‘a’ and ‘b’.
Can anyone help me with mnaking that program show ‘a’ only when ‘a’ was inserted value and ‘b’ only when be was inserted value?
<?php //myIsset.php
$a = '';
$b = '';
if (isset($_GET['a'])) echo 'a<br>';
if (isset($_GET['b'])) echo 'b';
echo '
<!DOCTYPE html>
<html lang = "en">
<head>
<title>myIntval.php</title>
<meta charset="utf-8" />
</head>
<body>
<form action = "myIsset.php" method = "GET">
A <input type ="text" name = "a">
B <input type ="text" name = "b">
<input type = "submit" value = "convert">
</form>
</body>
</html>';
?>
Yes, I missed that you were submitting the form using GET, so as @Mittineague says, both will be set. You’ll just need to test the value of each instead.
You have to understand what does isset mean. It means that a variable is set. And a variable that has been sent from a form, is apparently set. While whether it has any value or not is another matter.
<?php //myIsset.php
if (isset($_GET['submit'])) // to see if a form was submitted
{
if ($_GET['a'])) echo 'a'; // to actually see whether it has any value
if ($_GET['b'])) echo 'b';
}
?>
<!DOCTYPE html>
<html lang = "en">
<head>
<title>myIntval.php</title>
<meta charset="utf-8" />
</head>
<body>
<form action = "myIsset.php" method = "GET">
A <input type ="text" name = "a">
B <input type ="text" name = "b">
<input type = "submit" value = "convert">
</form>
</body>
</html>
there is a trick though. in case you enter 0, it won’t trigger an echo.