View Single Post
  #1 (permalink)  
Old December 27th, 2004, 01:02 PM
conscience conscience is offline
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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:

Code:
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:

Code:
if(!empty($_REQUEST["submit"]))
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:
Code:
$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:
Code:
<?php 
                    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:
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.

Thanks,
will
Reply With Quote