Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2008, 10:44 AM
twr twr is offline
Registered User
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default php5 chap 15 help needed

In the try/catch script, it will validate no problem but thats all. just returns the filled out form and not the text results.

the problem seems to be
 if (!is_object($e) and isset($posted)) {

    echo "Thanks for your entry. We'll be in touch.";

can anyone help with this please.
Regards

 
Old January 5th, 2008, 06:20 AM
twr twr is offline
Registered User
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Still not sorted this out, so i thought i should paste in the full script.

<html>
<head><title>Beginning PHP5</title></head>
<body bgcolor="#FFFFFF">
<?php
if (isset($_POST['posted'])) {

   //put the submitted values into regular variables

   $first_name = $_POST['first_name'];
   $last_name = $_POST['last_name'];
   $birth_date = $_POST['birth_date'];
   $phone = $_POST['phone'];
   $age = $_POST['age'];
   $address = $_POST['address'];
   $city = $_POST['city'];
   $state = $_POST['state'];
   $postal_code = $_POST['postal_code'];

    //make an array of field names and data types

    $field_names = array("first_name" => "string",
                "last_name" => "string",
                "birth_date" => "date",
                "phone" => "string",
                "age" => "integer",
                "address" => "string",
                "city" => "string",
                "state" => "string",
                "postal_code" => "string");

    //try checking the data type of each submitted value based on the name of the field

    function form_validate($fns) {

        foreach ($fns as $key => $value) {

            $field_value = $key;

            global $$field_value;

            //echo "actual field value is " . $$field_value . "<br>";

            switch ($value) {

                Case "string";

                    if ((strlen($$field_value) < 1) or (strlen($$field_value) > 99)) {
                        throw new Exception("Please enter a string value between 1 and 100 characters in the <b>$key</b> field");
                    }

                    break;

                Case "date";

                    if (!ereg("^[0-9]{4}\-([1-9]|(0[1-9])|(1[0-2]))\-([1-9]|(0[1-9])|([1-2][0-9])|3[0-1])$",$$field_value)) {

                        throw new Exception("Please enter a valid date formatted as YYYY-MM-DD in the <b>$key</b> field");
                    }

                    break;

                Case "integer";

                    if (!is_numeric($$field_value)) {

                        throw new Exception("Please enter a number without decimal places or alphabetical characters in the <b>$key</b> field.");
                    }

                    break;

                default;

                    break;

            }
        }
    }

    //catch the exception and produce an error message

    try
    {
        form_validate($field_names);
    }

    catch (Exception $e)
    {
        echo $e->getMessage();
        echo "<br>";

    }

}

//if no errors thank the user
 if (!is_object($e) and isset($posted)) {

    echo "Thanks for your entry. We'll be in touch.";
} else {

//return the filled out form to the user and ask them to try again

?>






Similar Threads
Thread Thread Starter Forum Replies Last Post
chap 15 manal_sag BOOK: Beginning ASP 3.0 2 July 10th, 2005 03:29 AM
help in chap 15 manal_sag BOOK: Beginning ASP 3.0 2 May 25th, 2005 02:59 AM
Pro PHP5, Ch.15, Class file usage BillD Pro PHP 0 April 7th, 2005 11:08 PM
Professional PHP5 Ch. 15 Custom Session Handler superrobotpope Pro PHP 4 February 4th, 2005 08:32 PM
Sessions - Chap 15 mminnie BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 January 10th, 2005 04:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.