nootkan Posted October 10, 2020 Share Posted October 10, 2020 One of the pursue tasks asked to do is: "rewrite one of the versions handle_reg.php so that it prints the users favorite color selection in the users favorite color. The hint says to use css and concatenation. I've tried a few things but can't seem to understand how to figure out the logic for this task. Below is one of the methods I tried that didn't work and I tried to debug for hours with no success. I also tried slashing the double quotation marks in the print command and add the paragraph tag to each style with no success. Please advise as to what I am not understanding. handle_reg.php file <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Registration</title> <style type="text/css" media="screen"> .error { color: red; } </style> </head> <body> <h1>Registration Results</h1> <?php // Script 6.7 - handle_reg.php #6 /* This script receives seven values from register.html: email, password, confirm, year, terms, color, submit */ // Address error management, if you want. // Flag variable to track success: $okay = TRUE; // Validate the email address: if (empty($_POST['email'])) { print '<p class="error">Please enter your email address.</p>'; $okay = FALSE; } // Validate the password: if (empty($_POST['password'])) { print '<p class="error">Please enter your password.</p>'; $okay = FALSE; } // Check the two passwords for equality: if ($_POST['password'] != $_POST['confirm']) { print '<p class="error">Your confirmed password does not match the original password.</p>'; $okay = FALSE; } // Validate the year: if ( is_numeric($_POST['year']) AND (strlen($_POST['year']) == 4) ) { // Check that they were born before 2011. if ($_POST['year'] < 2011) { $age = 2011 - $_POST['year']; // Calculate age this year. } else { print '<p class="error">Either you entered your birth year wrong or you come from the future!</p>'; $okay = FALSE; } // End of 2nd conditional. } else { // Else for 1st conditional. print '<p class="error">Please enter the year you were born as four digits.</p>'; $okay = FALSE; } // End of 1st conditional. // Validate the terms: if ( !isset($_POST['terms'])) { print '<p class="error">You must accept the terms.</p>'; $okay = FALSE; } // Validate the color: if ($_POST['color'] == 'red') { $color_red = 'red'; } elseif ($_POST['color'] == 'yellow') { $color_yellow = 'yellow'; } elseif ($_POST['color'] == 'green') { $color_green = 'green'; } elseif ($_POST['color'] == 'blue') { $color_blue = 'blue'; } else { // Problem! print '<p class="error">Please select your favorite color.</p>'; $okay = FALSE; } // If there were no errors, print a success message: if ($okay) { print '<p>You have been successfully registered (but not really).</p>'; print "<p>You will turn $age this year.</p>"; print "<p>Your favorite color is <style="color:red;">$color_red .''. <style="color:yellow;">$color_yellow . '' . <style="color:"green;">$color_green . '' . <style="color:blue;">$color_blue color.</p>"; } ?> </body> </html> Link to comment Share on other sites More sharing options...
Necuima Posted October 11, 2020 Share Posted October 11, 2020 Hi, You have a problem with the double quotes in your final print statement. Your first statement will end at the color:red unless you escape the double quotes around that style or use single quotes at that point. Cheers, Necuima Link to comment Share on other sites More sharing options...
nootkan Posted October 13, 2020 Author Share Posted October 13, 2020 Thanks Necuima, I did try that also with no success. I think I mentioned in my first post. I'll keep digging. Link to comment Share on other sites More sharing options...
nootkan Posted October 13, 2020 Author Share Posted October 13, 2020 (edited) 40 minutes ago, nootkan said: Thanks Necuima, I did try that also with no success. I think I mentioned in my first post. I'll keep digging. Here is the code I tried: // Validate the color: if ($_POST['color'] == 'red') { $color_red = 'red'; } elseif ($_POST['color'] == 'yellow') { $color_yellow = 'yellow'; } elseif ($_POST['color'] == 'green') { $color_green = 'green'; } elseif ($_POST['color'] == 'blue') { $color_blue = 'blue'; } else { // Problem! print '<p class="error">Please select your favorite color.</p>'; $okay = FALSE; } // If there were no errors, print a success message: if ($okay) { print '<p>You have been successfully registered (but not really).</p>'; print "<p>You will turn $age this year.</p>"; print "<p>Your favorite color is <span style=\"color:red;\">$color_red</span> . '' . <span style=\"color:yellow;\">$color_yellow</span> . '' . <span style=\"color:green;\">$color_green</span> . '' . <span style=\"color:blue;\">$color_blue color.</span></p>"; } Here is the error I am trying to understand how to fix: Edited October 13, 2020 by nootkan type o Link to comment Share on other sites More sharing options...
Necuima Posted October 14, 2020 Share Posted October 14, 2020 if the color is not red, then $color_red is not getting defined. You need something like: $color_red = ""; $color_yellow = ""; $color_green = ""; $color_blue = ""; Up front before you do the tests so that the variables are defined even if they are empty. Link to comment Share on other sites More sharing options...
Larry Posted October 14, 2020 Share Posted October 14, 2020 I wouldn't create multiple $color_ variables. Just create and use one $color variable. Link to comment Share on other sites More sharing options...
nootkan Posted October 15, 2020 Author Share Posted October 15, 2020 Thanks Necuima, but this is still confusing. I thought that $color_red = 'red'; was defining the variables. I don't understand why I have to define the variables if they're empty. If the color is picked and validated than the color gets printed doesn't it? I am missing something and I can't seem to get my head around it. Will keep trying to try different ways of defining the variables. Link to comment Share on other sites More sharing options...
nootkan Posted October 15, 2020 Author Share Posted October 15, 2020 On 10/14/2020 at 6:04 AM, Larry said: I wouldn't create multiple $color_ variables. Just create and use one $color variable. Hi Larry, thanks for your reply. I was just using the handle_reg.php code like the pursue task stated. Was I supposed to delete the if else statements and try something different? If so, I am not sure I understand how to figure this out. I have gone back and re-read the previous 6 chapters over a few times and I am still not sure how to figure out how to tell php that I want to just print out the chosen color. My head is spinning so I think I will take a break and see if I can clear my head and try again in a couple of days. Maybe I should continue to work through chapter seven and come back to the pursue tasks in chapter six? Appreciate the support from both of you as I try to get a better grasp of how to understand the thought process of figuring out which way to go when defining the solution. Link to comment Share on other sites More sharing options...
Necuima Posted October 16, 2020 Share Posted October 16, 2020 (edited) Hi Nootkan, The issue that I am trying to help you with is the definition of the PHP variables whose names start with $color_ For example, unless $_POST['color'] == 'red') is true, the PHP variable $color_red will not get set. But you try and use it in the print statement whether it is defined or not. But you are better off following Larry's guidance and set just one $color variable. Cheers Edited October 16, 2020 by Necuima Checking typing Link to comment Share on other sites More sharing options...
Larry Posted October 16, 2020 Share Posted October 16, 2020 I would always promote the benefits of walking away and returning when it starts to get muddled. Absolutely move on in the book and then come back to this in a bit! Link to comment Share on other sites More sharing options...
Recommended Posts