By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,248 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,248 IT Pros & Developers. It's quick & easy.

Initial value form field generation problem

P: n/a
I've got a system that automatically generates a form. I have it set
up so that the backend will return to the inital form page with an
error object in sessions data (assuming the backend detected invalid
data or required fields not filled in) and the frontend will generate
an initial value of items WITHOUT errors (so the user doesn't have to
reenter valid info).

This is the problem. The frontend generates the initial value
attribute 'value="(<field_name_here>)"', so if the field's name was
'Phone' it would generate 'value="(Phone)"' instead of
'value="555-555-5555"'.

I've checked what the POST method is sending the backend, and it's ok.
I also checked what the backend is sending the frontend through the
session varialbes, and it's ok too. So my only thought is it's either
somewhere in the code or it's MSIE 6.
Here's the code

It is based upon 3 objects:

1) fieldArray - matrix associative array of all fields of the form and
eache fields properties (ie. type, name, initial value, etc)
2) error->errorArray - array in error class with the names of each
error producing field (ie. required field not filled in, fields with
invalid data, etc.)
3) error->hasErrors - boolean, true if there are errors in errorArray
4) 3 objects (account,order,signup) each with an array of data from the
user (eventually put into mySQL db)

NOTE: this is in a foreach statement that walks the fieldArray

-----PHP Code-----
//check for type, if text, do this...
elseif($fieldArray[type] == 'text') {

//begin input element
$_element = "<input type=\"text\" name=\"$fieldArray[name]\" ";

//check for errors - if there are errors, but this field isn't in
// the list of fields with errors, do this...
if($error->hasErrors() &&
array_search($fieldArray[name],$error->getErrorArray())==false) {

//if data is in the account object, add the value attribute to
// the input field with the value from the object as the
// attribute value
//NOTE: the getField functions return false if the field isn't
// found, they return the value of the field if it is found
if($account->getField($fieldArray[name]) != false) {
$_element .= "value=\"$account->getField($fieldArray[name])\" ";
}

//if data is in the orderobject, add the value attribute to
// the input field with the value from the object as the
// attribute value
elseif($order->getField($fieldArray[name]) != false) {
$_element .= "value=\"$order->getField($fieldArray[name])\" ";
}

//if data is in the signupobject, add the value attribute to
// the input field with the value from the object as the
// attribute value
elseif($signup->getField($fieldArray[name]) != false) {
$_element .= "value=\"$signup->getField($fieldArray[name])\" ";
}
}

// finish the input field
$_element .= '/>';
}
-----PHP Code-----

Thanks for taking a look at this,
-TekWiz

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
TekWiz wrote:
I've got a system that automatically generates a form. I have it set
up so that the backend will return to the inital form page with an
error object in sessions data (assuming the backend detected invalid
data or required fields not filled in) and the frontend will generate
an initial value of items WITHOUT errors (so the user doesn't have to
reenter valid info).

This is the problem. The frontend generates the initial value
attribute 'value="(<field_name_here>)"', so if the field's name was
'Phone' it would generate 'value="(Phone)"' instead of
'value="555-555-5555"'.

I've checked what the POST method is sending the backend, and it's ok.
I also checked what the backend is sending the frontend through the
session varialbes, and it's ok too. So my only thought is it's either
somewhere in the code or it's MSIE 6.
Here's the code

It is based upon 3 objects:

1) fieldArray - matrix associative array of all fields of the form and
eache fields properties (ie. type, name, initial value, etc)
2) error->errorArray - array in error class with the names of each
error producing field (ie. required field not filled in, fields with
invalid data, etc.)
3) error->hasErrors - boolean, true if there are errors in errorArray
4) 3 objects (account,order,signup) each with an array of data from the
user (eventually put into mySQL db)

NOTE: this is in a foreach statement that walks the fieldArray

-----PHP Code-----
//check for type, if text, do this...
elseif($fieldArray[type] == 'text') {

//begin input element
$_element = "<input type=\"text\" name=\"$fieldArray[name]\" ";

//check for errors - if there are errors, but this field isn't in
// the list of fields with errors, do this...
if($error->hasErrors() &&
array_search($fieldArray[name],$error->getErrorArray())==false) {

//if data is in the account object, add the value attribute to
// the input field with the value from the object as the
// attribute value
//NOTE: the getField functions return false if the field isn't
// found, they return the value of the field if it is found
if($account->getField($fieldArray[name]) != false) {
$_element .= "value=\"$account->getField($fieldArray[name])\" ";
}

//if data is in the orderobject, add the value attribute to
// the input field with the value from the object as the
// attribute value
elseif($order->getField($fieldArray[name]) != false) {
$_element .= "value=\"$order->getField($fieldArray[name])\" ";
}

//if data is in the signupobject, add the value attribute to
// the input field with the value from the object as the
// attribute value
elseif($signup->getField($fieldArray[name]) != false) {
$_element .= "value=\"$signup->getField($fieldArray[name])\" ";
}
}

// finish the input field
$_element .= '/>';
}
-----PHP Code-----

Thanks for taking a look at this,
-TekWiz

Yup - I think i know the problem, assuming i've interpreted your post
correctly you want to be able perform server side validation and then
redirect the user to a redrawn form with validation error messages +
show the user the content of the form as the user submitted it.

To do this you have to use objects and sessions and pass an object that
encapsulates the form in the session. sounds complicated but it a'int
really, you just have cope with the concept of an asynchrounous
environment.

If you want some help i'll put some php source on my website that may
help. email me.

Chris

Jul 17 '05 #2

P: n/a
> Yup - I think i know the problem, assuming i've interpreted your post
correctly you want to be able perform server side validation and then redirect the user to a redrawn form with validation error messages +
show the user the content of the form as the user submitted it.

To do this you have to use objects and sessions and pass an object that encapsulates the form in the session. sounds complicated but it a'int really, you just have cope with the concept of an asynchrounous
environment.

If you want some help i'll put some php source on my website that may help. email me.

Chris


Sorry about the misunderstanding, but I've already got the sessions
part down. It passes everything properly, but it doesn't print it
properly. ie. instead of printing <input name="name" type="text"
value="John Doe" /> (where John Doe is in the object), it prints
<input name="name" type="text" value="(Name)" /> (literally, with the
parentases)

Jul 17 '05 #3

P: n/a
::Solved::

The problem here is that PHP doesn't like methods in strings (I was
treating them as variables; which they weren't.

The solution is to use the concatonation function '.' between the
string and the functions.

--TekWiz

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.