Jump to content
Larry Ullman's Book Forums

billm

Members
  • Posts

    4
  • Joined

  • Last visited

Posts posted by billm

  1. Oops. I should have put in some /n to make the source code readable. Thought of it just after I posted. Apologies.

     

    Thanks for the comments and for taking the time to reply.

    The double quotes was just silly of me.

    I wish I had thought of using the number _format that way. Just good style from a pro.

    I shall take on board the variable names comments.

    I have now got some more. The code for the ch 2 review and pursue is getting kind of big for posting here but I shall await your feedback on this. I suppose if it is just copied and pasted into and IDE it will be more readable.

    I hope this may be of some use to someone.

    I await your feedback with trepidation.

     

     

    PHP version?

     

    <!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>PHP Version</title>

     

    </head>

    <body>

    <?php #script chapter2 review and pursue page 73

     

    phpinfo();//gives all details of php installed

    echo '<br /> PHP version is ' . PHP_VERSION; //just the version

    ?>

    </body>

    </html>

     

    All the rest of the form and handler:

     

    pursue_form.html

     

    <!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>HTML Feedback Form</title>

    <style type="text/css" title="text/css" media="all" >

    label,legend{

    font-weight: bold;

    color: #300acc;

    }

    form{

    background-color: #cccccc;

    }

    </style>

    </head>

    <body>

    <!--script 2.1 form.html-->

     

    <form action="handle_pursue_form.php" method="post">

     

    <fieldset><legend>Please enter your information in the form below:</legend>

    <p><label>Please complete all entries.</label></p>

    <p><label>First Name: <input type="text" name="fname" size="20" maxlength="40" /></label></p>

    <p><label>Last Name: <input type="text" name="lname" size="20" maxlength="40" /></label></p>

    <p><label>Email address: <input type="text" name="email" size="40" maxlength="60" /></label></p>

    <p><label for="gender">Gender: </label>

    <input type="radio" name="gender" value="male" /> Male

    <input type="radio" name="gender" value="female" /> Female</p>

    <p><label>Age:

    <select name="age">

    <option value="0-24">Under 25</option>

    <option value="25-64">Between 30 and 60</option>

    <option value="65+">Over 60</option>

    </select></label></p>

    <p><label>How many times have you used the garage?

    <select name="use">

    <option value="1">First time</option>

    <option value="<6">2 to 5 times</option>

    <option value=">5">More than 5 times</option>

    </select></label></p>

    <p><label>How did we do? (choose one only): </label></p>

    <p><input type="checkbox" name="quality[]" value="poor" />Poor</p>

    <p><input type="checkbox" name="quality[]" value="average" />Average</p>

    <p><input type="checkbox" name="quality[]" value="good" />Good</p>

    <p><input type="checkbox" name="quality[]" value="great" />Great</p>

    <p><input type="checkbox" name="quality[]" value="excellent" />Excellent</p>

     

    <p><label>What work did we do on your last visit? Choose all that apply: </label></p>

    <p><input type="checkbox" name="service[]" value="mot" />MOT</p>

    <p><input type="checkbox" name="service[]" value="mot repairs" />MOT Repairs</p>

    <p><input type="checkbox" name="service[]" value="air con recharge" />Recharge Air Conditioning</p>

    <p><input type="checkbox" name="service[]" value="tyres" />Tyres</p>

    <p><input type="checkbox" name="service[]" value="car servicing" />Car Servicing</p>

    <p><input type="checkbox" name="service[]" value="other work" />Other Work</p>

     

    <p><label>Any comments? What can we improve on? <textarea name="comments" rows="3" cols="40"></textarea></label></p>

    </fieldset>

    <p align="center"><input type="submit" value="Submit Information" /></p>

    </form>

    </body>

    </html>

     

    handle_pursue_form.php

     

    <!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>Form Feedback</title>

    <style type="text/css" title="text/css" media="all" >

    label,legend{

    font-weight: bold;

    color: #300acc;

    }

    form{

    background-color: #cccccc;

    }

    .error{

    color: #C00;

    font-weight: bold;

    }

    </style>

    </head>

    <body>

    <?php #script page 73 pursue. I have concatenated most of the page 73 questions in this one script

     

    //validate the gender:

    //rewrite gender conditional

    //is this what was asked for or have I misunderstood the question?

     

    //was anything selected and was it male or female

    if(isset($_POST['gender']) && (($_POST['gender']=='male')) ){//value checked AND male

    //value checked

    $gender = $_POST['gender'];//male

    $title = 'Mr.';

     

    }elseif(isset($_POST['gender']) && $_POST['gender']=='female'){//value checked AND female

     

    $gender = $_POST['gender']; //female

    $title = 'Ms.';

     

    }else{//radio button not checked

     

    $gender = NULL;

    $title_error = '<p class="error">Gender should be either male or female.</p>';//error message

     

    }

     

    //validate the first name

    if (!empty($_POST['fname'])){//first name entered

     

    $fname = $_POST['fname'];

     

    }else{//nothing entered in the box

     

    $fname = NULL;

    $fname_error = '<p class="error">You did not enter your first name.</p>'; //error message

     

    }

     

    //validate the last name

    if (!empty($_POST['lname'])){//last name entered OK

     

    $lname = $_POST['lname'];

     

    }else{//no last name entered

     

    $lname = NULL;

    $lname_error = '<p class="error">You did not enter your last name.</p>';//error message

     

    }

     

    //validate number of times garage used

    //couldn't resist trivial use of switch function

    if ((isset($_POST['use']))){

    $use = $_POST['use'];

    switch ($use) {

    case '1':

    $greeting = 'Nice to see a new customer ';

    break;

    case '<6':

    $greeting = 'Great to see you back again';

    break;

    case '>5':

    $greeting = 'Welcome back ';

    break;

    default:

    $greeting_error = '<p class="error">You have not told us how many times you have used the garage.</p>';//no number of visits selected error message

    break;

    }

     

    }else{//error message

     

    $greeting_error = '<p class="error">Please indicate how many times you used the garage</p>';//no number of visits selected error message

     

    }

    //validate email

    if(!empty($_POST['email'])){ //if the box is not empty

     

    $email = $_POST['email'];

     

    }else{//no mail address entered

     

    $email = NULL;

    $email_error = '<p class="error">You did not enter your email.</p>';

     

    }

     

    //validate the comments

    if(!empty($_REQUEST['comments'])){//if the box is not empty

     

    $comments = $_REQUEST['comments'];

     

    }else{//no comments entered

     

    $comments = NULL;

    $comments_error = '<p class="error">You made no comments.</p>';//no comment error message

     

    }

     

    //validate age

    if (isset($_POST['age'])){//selection made

     

    $age = $_POST['age'];

     

    if($age=='0-24'){

     

    $age_message = '<p>Your age is given as under 25 years old. You may qualify for a student discount.</p>';

     

    }elseif ($age =='25-64'){

     

    $age_message = '<p>Your age is given as between 25 and 60. If you are in part time employment or unemployed, you may qualify for a reduced rate.</p>';

     

    }elseif ($age == '65+'){

     

    $age_message = '<p>Your age is given as above 65 years old, so you may qualify for our pensioners discount.</p>';

     

    }else{

     

    $age = NULL;

    $age_error = '<p class="error">Please choose a value for your age.</p>';//no age chosen error message

     

    }

    }else{//nothing selected

     

    $age = NULL;

    $age_error = '<p class="error">You did not select an age.</p>';//error message

     

    }

     

     

    //how did we do? Array. If more than one box ticked give error message

    if (isset($_POST['quality'])){//at least one box ticked

     

    $quality = $_POST['quality'];

     

    $num = count($quality); //trivial use of count: better as radio button

     

    if ($num == 1){//if just one box ticked

     

    foreach ($quality as $k => $v){

    $quality_ok = '<p>You have found our service to be ' . $v . '.</p>';//this only works because only one value can be printed

    }

     

     

    }else{//too many boxes ticked

     

    $quality = NULL;

    $quality_error = '<p class="error">You ticked ' . $num . ' boxes. Please make only a single choice for quality of service.</p>';//error message using count function

     

    }

    }else{//no box ticked

     

    $quality = NULL;

    $quality_error = '<p class="error">You made no choice of quality of service. Please tick one box.</p>';//error message

     

    }

     

    //what services did we provide? Array

    if (isset($_POST['service'])){//if box/boxes ticked

     

    $service = ($_POST['service']);

    sort ($service);//sort in alphabetical order

    //shuffle($service);//randomly sort the list

    $work_done = implode (' <br />', $service);//turn array into string

    $work = '<p>We provided the following services last time you visited: <br /> ' . $work_done . '</p>';

     

    }else{

     

    $service = NULL;

    $work_error = '<p class="error">Please choose at least one service.</p>';//error message

     

    }

     

    //if all OK, display information

    if($fname && $lname && $greeting && $email && $age && $gender && $use && $comments && $quality && $service){

     

    echo '<p>' . $greeting . ' ' . $title . ' ' . $fname . ' ' . $lname . ' and thank you for your comments:

    <br /><br /><tt>' . $comments .'</tt> '. $work . $quality_ok . $age_message . '

    We will reply to you at <i>'. $email . '</i></p>';

     

    }else{//if it goes wahoonee shaped display error messages

     

    echo $title_error . $fname_error . $lname_error . $greeting_error . $age_error . $work_error .$quality_error . $comments_error . $email_error .

    '<p class="error">Please go back and complete the form.</p>';//concatenate error messages

    }

     

    ?>

    </body>

    </html>

    Hello I'm new around here and i was having trouble with the review and pursue chapter 2. I tried your code Graham and i was getting these errors when executing" Notice: Undefined variable: greeting_error in /var/www/html/php_sandbox/RnP_Chap2.php on line 194

     

    Notice: Undefined variable: age_error in /var/www/html/php_sandbox/RnP_Chap2.php on line 194

     

    Notice: Undefined variable: work_error in /var/www/html/php_sandbox/RnP_Chap2.php on line 194". I am seeing for my self that the variables are defined but i don't understand why it's giving me these errors. Please i will need some guidance.

×
×
  • Create New...