...

Registering via a form problem

Michiel
07-17-2002, 01:17 PM
Hi,

on my website people can register via a form. After they submit the form, their answers are checked and if all ok their info is stored in a db. If there are errors, the rigistration form is shown again with the correct answers already filled in. At least that is how I want it to be :(. I'm experiencing some problems on letting the script 'remember' the correct answers the seccond time the page is loaded. This code shows the structure of my script:


if ($action == "explanation") {

print("blah blah explanation");

}

else if ($action == "form") {

if (IsSet($error)) {$color="red";}
else {$color="black";}

print("<FORM action=$PHP_SELF method=post>
<INPUT type=hidden name=action value=check><BR>
<FONT color=$color>Name: <INPUT type=text name=name value=$name><BR>
<FONT color=$color>Street: <INPUT type=text name=street value=$street><BR>
<FONT color=$color>Zip-code: <INPUT type=text name=zipcode value=$zipcode><BR>
<INPUT type=submit>
</FORM>");
}

else if ($action == "check") {

$error = "0";

//Check name
if (!IsSet($name) {$error++}
// Check street
// Check zipcode, also check format, if format is incorrect unset $zipcode

if ($error == "0") {

// Put everything in db

}

if ($error !== "0") {

// reload page with $action set to form, $error, $name, $street and $zipcode
// How this is done is exactly the problem ....
}
}



Can anyone help me with this??

Thanx, Michiel

mordred
07-17-2002, 02:00 PM
I believe your most important error is that you omit the quotes around values of attributes. If the values that shall be shown in the input fields contain any whitespace, the field will only show a truncated version since the browser can't render it properly then.

Try if


print("<FORM action='$PHP_SELF' method='post'>
<INPUT type='hidden' name='action' value='check'><BR>
<FONT color='$color'>Name: <INPUT type='text' name='name' value='$name'><BR>
<FONT color='$color'>Street: <INPUT type='text' name='street' value=$street><BR>
<FONT color='$color'>Zip-code: <INPUT type='text' name='zipcode' value='$zipcode'><BR>
<INPUT type='submit'>
</FORM>");


works better for you.

I am aware that a great amount of beginner tutorials and even some books favor a HTML style without quotes around attribute values. IMO you should always try to write proper HTML, but that's only my opinion... in your case it can be even necessary to do so.

BTW: It's no real error, but usually one increments an integer, not a string (the $error variable). Only due to PHPs type-juggling feature that does not constitute a problem.

Michiel
07-17-2002, 07:06 PM
Hi Mordred,

you are right about the quotes, in had just put this example code all together very quickly and just 'forgot' about the quotes. My real script indeed has got them. But this does not answer my real question. The problem I'm having is that I don't know how the script can 'remember' the variables when the check shows that not all the fields are filled in right. This means that the page must be reloaded with $action set to form again. Now I want the script to fill in the answers that were already filled in correctly. How does one do that??

Michiel

mordred
07-18-2002, 06:54 PM
Sorry Michiel, my bad, should've read your code comments better to understand the main problem... anyway, I've rewritten your script example in a way that should accomplish what you need. Feel free to ask if anything seems obscure, but I think the comments are pretty much self-explanatory:


// Note: This line I only had to test with a form that gets first opened
// with URI?action=form, and then the POST request is sent to the same page.
// Thus I needed to set the $action variable according to which request
// method was used to pass this variable
$action = isset($_POST["action"]) ? ($_POST["action"]) : ($_GET["action"]);

// this array stores all fields that have not been filled out
$error = 0;

// array that stores the color for the various input elements
$color = array(
"name" => "black",
"street" => "black",
"zipcode" => "black"
);

// and now, print different things according to action
if ($action == "explanation") {
// prints out explanation
print("blah blah explanation");
} elseif ($action == "form") {

// the form HTML
$output = "<form action='$_SERVER[PHP_SELF]' method='post'>\n";
$output .= "<input type='hidden' name='action' value='check'><br />\n";
$output .= "<span style='color: $color[name]'>Name: </span>";
$output .= "<input type='text' name='name' /><br />\n";
$output .= "<span style='color: $color[street]'>Street: </span>";
$output .= "<input type='text' name='street' /><br />\n";
$output .= "<span style='color: $color[zipcode]'>Zip-code: </span>";
$output .= "<input type='text' name='zipcode' /><br />\n";
$output .= "<input type='submit' />\n";
$output .= "</form>\n";

// print the standard form
print($output);
} else if ($action == "check") {

// the various fields get checked. If a field is not set,
// then the form is printed again with red fieldnames and
// nothing is stored in the db

// Check if name has been filled out
if (!isset($_POST["name"]) || strlen($_POST["name"]) == 0) {
$error++;
$color["name"] = "red";
}
if (!isset($_POST["street"]) || strlen($_POST["street"]) == 0) {
$error++;
$color["street"] = "red";
}
if (!isset($_POST["zipcode"]) || strlen($_POST["zipcode"]) == 0) {
$error++;
$color["zipcode"] = "red";
}

if ($error == 0) {
// store the data in the db
// ...
// just to see that it works, it prints out a message
print("data seemed ok");
} else {
// print again the standard form - but now the colors for
// the fields that were not filled have been changed to red
$output = "<form action='$_SERVER[PHP_SELF]' method='post'>\n";
$output .= "<input type='hidden' name='action' value='check'><br />\n";
$output .= "<span style='color: $color[name]'>Name: </span>";
$output .= "<input type='text' name='name' value='$_POST[name]'/><br />\n";
$output .= "<span style='color: $color[street]'>Street: </span>";
$output .= "<input type='text' name='street' / value='$_POST[street]'><br />\n";
$output .= "<span style='color: $color[zipcode]'>Zip-code: </span>";
$output .= "<input type='text' name='zipcode' value='$_POST[zipcode]'/><br />\n";
$output .= "<input type='submit' />\n";
$output .= "</form>\n";

print($output);
}
}

Michiel
07-18-2002, 09:33 PM
Hi Mordred,

thanx for your reply, this realy helps me a lot. This method works just fine! I've changed one bit to make the script more efficient, at least it is easier to make changes at a later stage. Since my form is quite a bit larger than my example, I've decided to put my form in an external file and then include it into my script. That way I only have to edit the form once when I need to make some changes.

Well, thanx anyway :thumbsup:

Michiel



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum