PDA

View Full Version : missing a required field



chalexan
09-04-2004, 01:07 PM
If I miss a required field, when I am adding a listing, I get an error message telling me that I forgot to fill in a required field. When I go back, all of the data that I filled in is gone. Is there a way to keep the data so I don't have to type in all of the data again?

the_sandking
09-04-2004, 01:38 PM
There's nothing built-in to OR currently that handles this type of situation.

One possibility would be to add some javascript form validation script to the page(s) you want to retain the variables.

chalexan
09-04-2004, 03:12 PM
Thanks for the reply. I have been looking into JavaScript, but because these forms are dynamic, I have been unable to figure out a way to know, through JavaScript, if the fields are required or not. The JavaScript forms object, as far as I can tell, can get the name, value, and type. If I could figure out how to get the label, then I could just search it for an *, but no such luck yet.

Thanks for any other suggestions/solutions?

chalexan
09-06-2004, 01:46 AM
Ok. Iíve got my site working good enough to post an algorithm.

The first thing I did was declared a few global variables:
$i=0;
$form_elements = array();
global $i, $form_elements;

Second, while I was looping through my database to renderFormElements, I filled an array with all of the elements that were required:
global $form_elements, $i;
if($required == "Yes")
{
$form_elements[$i++] = $field_name;
}

Third, I commented out the form submit button:
// renderFormElement("submit","","Submit", "", "", "");

Fourth, I converted my php array with a JavaScript array with a neat little function I found doing a Google search:
function j_array($var)
{
$javastr = "<script language=\"javascript\">";
$javastr .= "var d = new Array();";
while(list($key, $val) = each($var))
{
$javastr .= "d[$key] = '$val';";
}
$javastr .= "</script>";
return $javastr;
}
print "<html>";
print "</head>";
echo j_array($form_elements);
print "</head>";

Fifth, I made a new submit button that called a JavaScript function:
<INPUT TYPE="button" VALUE="Submit" onClick =javascript:submitform()">

Sixth, I created the function to handle the form validation:
<SCRIPT language="JavaScript">
function submitform()
{
var pass_the_form = true;
for(i=0; i<document.forms[0].elements.length; i++)
{
for(x=0; x<d.length; x++)
{
if(d[x] == document.forms[0].elements[i].name)
{
if(document.forms[0].elements[i].value == "")
{
pass_the_form = false;
}
}
}
if(document.forms[0].elements[i].value == "Choose One")
{
pass_the_form = false;
}
}
if(pass_the_form == true)
{
document.create_new_lead_form.submit();
}
else
{
alert("Please fill in all required fields");
}
}
</SCRIPT>

And that was all I had to do. Now there are some forms that had some static elements that I had to add to my array before I added the ones from the database, but it was as simple as:
global $form_elements, $i;
$form_elements[$i++] = "edit_user_name";
$form_elements[$i++] = "edit_user_pass";
$form_elements[$i++] = "edit_user_pass2";
$form_elements[$i++] = "user_email";

I hope this helps.

ican
10-09-2004, 12:13 AM
What files did you change

or can you just post the files you changd please.

I would like this mod if you don't mind.

pbflash
11-11-2004, 02:21 PM
Does anyone know where to insert this code to make it work?