Forums

Get value of form field value before form submit

jwagnerbds 23 Sep, 2009
I figure I need to use javascript for this. I want to get the value of a form field as soon as text is entered.

I tried using onChange=\"this.form.submit()\" in the <input> of the field. My form name is ChronoContact_ItemRegistration. My field name is promo

I want to assign the value of the field to a variable. Then I am going to use an if then else if else statement to assign value of another form field depending on what it entered in promo.

I am assigning the value of paypal's amount field depending on what is entered in the promo field.

How do get the value of the promo field into a variable before I submit my form.

Thanks in advance for your help.

Joel
Mizpah 09 Oct, 2009
untested (this is from the top of my head), but try somthing like this perhaps as a javascript function ?

[code=php]<span class="syntaxdefault"><br />getValue</span><span class="syntaxkeyword">()<br />var
GreyHead 09 Oct, 2009
Hi jwagnerbds,

A slightly modified version of Mizpah's code using MooTools syntax. This code goes into the Form HTML box. I assume that you have two fields - an input with an id of 'promo' and a hidden field with an id of 'promo_value':
<?php
$script = "
window.addEvent('domready', function() {
  var setpromo = function() {
    var promo_code = $('promo');
    var promo_value = $('promo_value');
    switch(promo_code) {
      case 'code_1':
        promo_value.value = 'value_1';
        break;
      case 'code_2':
        promo_value.value = 'value_2';
        break;
      default:
        promo_value.value = '';
    }
  };
  $('promo').addEvent('onBlur', setpromo);
});
";
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration($script);
?>
Not tested and will need de-bugging.

This style will load all the JavaScript in the page header and will attach the events to the input without you needing to add them in your Form HTML.

Bob
Mizpah 09 Oct, 2009
H Bob,

In reference to the above mootools version, is there an article on why the MooTools methods are better ? I am sure they are, I just havent played so much with the MooTools versions: I am now wondering if I need to convert about 8 different functions across a few forms!

@jwagnerdbs - apologies if I led you astray, please go with Bob's version!
GreyHead 09 Oct, 2009
Hi Mizpah,

There's a few things here:[list]
  • There's nothing fundamentally 'better' about the core script; though I find that MooTools provides some valuable shortcuts like $('input_id') for getElementById.input_id, and some useful added functionality. See the MooTools 1.1 docs for more. As MooTools is almost always loaded with ChronoForms it can usually be safely used.
  • The Joomla $doc syntax loads the code into the page header rather than into the middle of teh body (which is what the ChronoForms JavaScript box does). This validates better and sometimes executed better.
  • The addEvent('domready', . . . code defers execution until the bulk of the page is loaded which gets roudn some more potential problems. The standard 'onload' does more or less the same but the MooTools domready executes a little earlier without waitign for all the images etc. to load.
  • [/list]
    Hope this helps a bit.

    Bob

    PS MooTools 1.2 is quite a lot nicer and has some great extensions - but Joomla 1.5 uses the 1.1 version so it's safer to stay with that.
    Mizpah 09 Oct, 2009
    @bob It does! You know I thought forms were simple once upon a time - I never realised I would be getting into a world of dom, ajax, php and javascriptjust by trying to continually 'bake a better biscuit'. Thanks for your continued (above and beyond) the call of duty help - over time I really think that chronoforms is not only great working poduct, but an extensable platform from which I am slowly learning more and more!
    GreyHead 09 Oct, 2009
    Hi Mizpah,

    That's so true. Simple forms are very simple - then you think . . . "maybe I could do 'that' . . . " and suddenly it's a whole lot richer. But mostly you can do 'that' with a little ingenuity.

    Bob
    This topic is locked and no more replies can be posted.