Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi first time post - just got the book a couple of days ago - the book is great!

 

Struggling to understand this though..

 

Step six asks you to write this script:

 

//check comments were entered.
if (!empty($_REQUEST['comments'])){
$comments = $_REQUEST['comments'];
} else {
$comments = NULL;
echo '<p class ="error">What you don\'t want to comment afterall?</p>';
}
//validate gender variable.
if (isset($_REQUEST['gender'])){
$gender = $_REQUEST['gender'];
if ($gender == 'M') {

$greeting = '<p><b>Good day, sir!</b></p>';

} elseif ($gender == 'F') {

$greeting = '<p><b>Good day, Madam!</b></p>';

} else {
$gender = NULL;
echo '<p class = "error">Gender should be either "M" or "F"!</p>';

}

 

Why would you create $greeting which is different depending on the answer stored in the $gender when echo would do? Am I missing something here? Also its the first time $greeting has been mentioned in this script and I can't see it referenced elsewhere which up until this point when a new variable has been introduced the book has explained what it is storing or doing.

 

Help!

 

One other thing - if the user writes in the comments box the following: 'I didn't realise it was Sunday today' it is output after successful submission as 'I didn\'t realsie is was Sunday today'

 

Anyway to fix this so that the \ isn't output?

Edited by JasonHegarty
Link to comment
Share on other sites

Regarding your second question, your system probably has magic quotes turned on. You can look up magic quotes in the php manual - even though it is deprecated, a lot of hosts keep it enabled. To override it you can check for it and then use stripslashes() e.g.

 

$comments = (get_magic_quotes_gpc() ? stripslashes($_REQUEST['comments']) : $_REQUEST['comments']);

  • Upvote 2
Link to comment
Share on other sites

Regarding your second question, your system probably has magic quotes turned on. You can look up magic quotes in the php manual - even though it is deprecated, a lot of hosts keep it enabled. To override it you can check for it and then use stripslashes() e.g.

 

$comments = (get_magic_quotes_gpc() ? stripslashes($_REQUEST['comments']) : $_REQUEST['comments']);

 

Thanks for this margaux worked a treat!

Link to comment
Share on other sites

 Share

×
×
  • Create New...