Topic: Change form field value

Hi,
 
   I have been work deeper with forms instead of IDs to get and set values. Because on bigger projects you should create to many IDs and take care of each is harder besides form.

   Does xajax has something:

$objResposta->assign(formname,fieldname,'value',newvalue);

?

Best Regards,
Guilherme Filho

Re: Change form field value

Hi,

Xajax does not have such a feature. However, ids are supposed to be unique on a web page so assigning new values shouldn't be any more difficult than $response->assign($id,$attribute,$data);

I think what would be nice is the ability to pass some json to the browser and have xajax javascript populate all of the field values. But that might not happen... You could, however, write a javascript function that receives a json string and assigns values from the string to the fields. Of course you still need a unique identifier for the fields.

The current xajax form field getFormValues() depends on field names and I always assign a field name and id using the same value. This makes it easier if you want to assign values in your response..

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Change form field value

Hi Ed.

    I have been working with XAJAX since 2007. Now my projects are too big, creating IDs for every field is getting hard to manage. I found much easier separate the fields by forms. Now I can have fields with the same meaning with the same name. On some pages there are more than one form and some times it has the same fields meaning.

    The json helped me to show the values for the users. Now I'm will feed the forms some forms. I will create some javascript to do it but maybe we can include this feature for the next version. I think it would be nice we can feed a form by the name using a native xajax's function.

    I haven't study XAJAX deeper to know how it seed and receives the data. Does it uses JSON to do it? I like to make the Javascript more light than possible. Do you think creating a native function for the feeding a form by form id, field name it will make the Javascript as big as some script I can develop?

Best Regards,
Guilherme Filho

Re: Change form field value

I know what you mean by projects getting too big but I would still recommend using ids and names. You may have just as much trouble keeping track of names only.

If you fed a form using field names is it ok to have 2 same name fields in one form?

Xajax uses xml or, in the latest version you can use json. You can always use json as a parameter to a js function that you write. I agree that a native xajax function would be nice but who knows when the next version will come out...

I will look into a js function that takes some json and populates a form. Kind of busy right now so it may take a bit of time.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Change form field value

Hi Ed,
   Thank's for your help. I wrote the code bellow... not so big as I tought;

function feedForm(formid, formvalue)
{
    var form = document.getElementById(formid);
    if (form)
    {
        for(fieldname in formvalue)
        {
            if (document.forms[formid][fieldname])
            {
            document.forms[formid][fieldname].value = formvalue[fieldname];
            }
        }
    }
}

   It's working... just remember that the JSON just accept UTF8 values.

Best Regards,
Guilherme Filho

Re: Change form field value

Nice!

So in your php you build an array then json encode it when ready to send it to the browser?

Is there any difference with elements like textarea or div? Not sure they all have a value attribute - text and innerHTML.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Change form field value

Yes. I create a array (field_name => value)... so I do a $jSON = json_encode($array_created); and $objResposta->script('feedForm(\'formid\','.$jSONcampos.');');

My idea was feeding a form, so I just tought about fields with INPUT tag. I'm pretty sure DIVs can't have value... and we can't access DIV or SPAN by document.forms[formid][divname or id]... About the textarea I'm not sure if I should use .innerHTML or .value... maybe we should add something as if(typeof element == 'textarea') use innerHTML.

To feed the divs I had created a similar function but besides .value I use .innerHTM and besides (formId, fieldName) I use (className). So I read the .children from some specific DIV and compare each element.className with the className received if equal I print the value.

Guilherme Filho