Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Server Side programming page 66 problem

Uploaded by

aalishvirani5522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Server Side programming page 66 problem

Uploaded by

aalishvirani5522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

USER INPUT AND CONDITIONS

16/11/2024
<html> <head>
<title>Form</title>
</head> <body>
<form method="post" action="handler.php">
<p>What is your name:</p>
<input type="text" name="username"></p>
<p>What is your favorite color:
<input type="radio" name="favoritecolor" value="r" />
Red <input type="radio" name="favoritecolor" value="g" />
Green <input type="radio" name="favoritecolor" value="b" />
Blue </p> <input type="submit" value="Submit" />
</form>
</body> </html> 66
USER INPUT AND CONDITIONS

16/11/2024
 Now we will use these inputs to create a page
that automatically changes background color
according to what the user's favorite color is.
 We can do this by creating a condition that uses
the data the user has filled out in the form.

67
<?php
$strHeading = "<h1>Hello " . $_POST["username"] .
"</h1>";
switch ($_POST["favoritecolor"]) {

16/11/2024
case "r": $strBackgroundColor = "rgb(255,0,0)"; break;
case "g"; $strBackgroundColor = "rgb(0,255,0)"; break;
case "b": $strBackgroundColor = "rgb(0,0,255)"; break;
default: $strBackgroundColor = "rgb(255,255,255)";
break;
}
?>
<html> <head> <title>Form</title> </head>
<body style="background: <?php echo
$strBackgroundColor; ?>;">
<?php echo $strHeading; ?> </body> </html> 68
USER INPUT AND CONDITIONS

16/11/2024
 The background color will be white if the user
has not chosen any favorite color in the form.
 This is done by using default to specify what
should happen if none of the above conditions are
met.
 But what if the user does not fill out his name?

 Then it only says "Hello" in the title.

 We will use an extra condition to change that.

69
<?php
$strUsername = $_POST["username"];
if ($strUsername != "") {
$strHeading = "<h1>Hello " . $_POST["username"] .

16/11/2024
"</h1>"; }
else { $strHeading = "<h1>Hello stranger!</h1> "; }
switch ($_POST["favoritecolor"]) {
case "r": $strBackgroundColor = "rgb(255,0,0)"; break;
case "g"; $strBackgroundColor = "rgb(0,255,0)"; break;
case "b": $strBackgroundColor = "rgb(0,0,255)"; break;
default: $strBackgroundColor = "rgb(255,255,255)";
break; }
?>
<html> <head> <title>Form</title> </head> <body
style="background: <?php echo $strBackgroundColor;
?>;"> <? echo $strHeading; ?> </body> </html>
70
USER INPUT AND CONDITIONS

16/11/2024
 In the example above, we use a condition to
validate the information from the user.
 In this case, it might not be so important if the
user did not write his name.
 But as you code more and more advanced stuff,
it's vital that you take into account that the user
may not always fill out forms the way you had
imagined.

71

You might also like