p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   Pro PHP (http://p2p.wrox.com/pro-php-96/)
-   -   Truncated Form Values. (http://p2p.wrox.com/pro-php/23130-truncated-form-values.html)

conscience December 27th, 2004 01:02 PM

Truncated Form Values.
Subject: Your basic PHP shopping cart system with back-end mailer.

Does it work?: Yes, if all client fields are filled correctly and simultaneously.

What's my problem?:

One, the form validates/submits on page load. Validation is conditional on:


if(isset($_REQUEST["submit"])) {
//validation, blah blah blah

"submit" is the name of the input type, which is also "submit". I've tried this with "$_POST" as well, and using various permutations of PHP like:


if($_POST['submit'] == 1)

And so forth.

This is ignored and the validation code is parsed. Validation errors pop on page load, etc. Simple enough.

Two: Not so simple. If entry is valid, the form should echo the value on the form line and store it in a PHP-echo'd hidden input just below (if invalid, display text field and error message). It does. However, if this value is separated by a whitespace, then it doesn't validate AGAIN once the person hits Submit after correcting other errors. I think this is a problem with my regular expressions, since all single-word values without whitespace are parsed and stored correctly on the page until the form is complete and valid.

Regex Examples:

$namepattern = '/^\b[A-Za-z]+\b\s\b[A-Za-z\-]+\b$/';
$titlepattern = '/^(Local\sBody\sMaster|Body\sMaster|Deputy\sBody\sMaster|Deputy\sMaster|Secretary|Initiation\sSecretary|Treasurer|LBM|DBM])$/';

Display Code:

                    if ($valid[1] == 0) {
                        if ($message[1] != "") {
                        echo "<span class=\"error\">" . $message[1] . "<//span><br>\n"; }
                        echo "<input type=\"text\" width=\"200\" name=\"name\" value=" . $_POST["name"] . ">"; }
                    elseif ($valid[7] == 1) {
                    echo $_POST["name"];
                    echo "<input type=\"hidden\" name=\"name\" value=" . $_POST["name"] . ">"; } ?>

$message is an empty array with null values for each field.
$valid is filled with zero values for each field.

Validation Code:

if (preg_match($namepattern, $_POST["name"]) > 0) {
    $message[0] = 0;
    $valid[0] = 1;
    } else {
    $valid[0] = 0;
    $message[0] = "Please enter your full name.";
    $class = "error"; }

Problem Example:

Person enters their name and hits Submit. Name is accepted and displayed. Person fills the rest of the form correctly and hits Submit. Form returns error and Name with last name and separating whitespace chopped. Person fixes Name and Submits. Form returns errors with the first words of every other string in place of the actual, already-validated string, but Name is valid. Rinse, wash, repeat, with any combination.

Of course, if it's filled right the first time then everything works great. If anything is a single-value, the variable is stored and displayed properly. I have a feeling that the two problems are ultimately related to each other.

Is there a difference between the data as entered by the user and the data as displayed by 'echo $_POST["name"];'? Do I need to use a preg_replace somewhere?

A related question:

Explain the difference in using "=", "==", and "===" here, especially in relation to the validation trigger. I've managed to confuse myself nicely between all this.

ANY assistance would be appreciated. Heavy emphasis on the ANY.


conscience December 27th, 2004 01:53 PM

Noted the lack of an additional escaped quote on the hidden inputs, which should solve the loss of data.

Automatic form submittal continues to be a problem.

All times are GMT -4. The time now is 08:19 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.