modifying a form text by a program

daffy 12 Apr, 2009
Hi,
I have been working on this for a few days guided by a reply given by BOB on Jan 31 2009 to a post by iaweb2.
I have a form for clients to submit when they want to hire a product. I want to show in text on that form whether the product is available. So I have the word 'Available' followed by 'yes' or 'No' or ' ? '. A three letter text in each case. The availability is unknown until the client has entered the dates he requires it after which the message is yes or no. I have a function 'isAvail()' which takes the dates and will return the three letter text.

I have bought second-hand what appears to be a very useful book 'DOM scripting' by J. Keith and this has initiated me into the mysteries of elements, nodes etc about which I was very hignorant. But it still does not work. The secret seems to be in window.addEvent('domready',function{....});

I think/hope I am in a position to write the function now but little things like
Where do I put the script that begins 'window.addEvent' ? and
How do I ensure that the necessary mootools library is included ?
beat me.

I am trying to test the form outside joomla until it works as I want.
--
Dave
ps this is the second time I have written this post this morning...it let me write it then said I was not logged in and I lost it !
So If there are two copies forgive me. :?
Max_admin 13 Apr, 2009
Hi Dave,

I think Bob has posted a complete code here in another topic ?

Cheers,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 13 Apr, 2009
Hi Dave,

Not sure I have a full answer but I can give you some pieces.

You can make sure that MooTools is loaded in a ChronoForms Form either by setting 'Enable Validation ?' to Yes on the Validation tab or by setting 'Load Chronoforms CSS/JS Files?' to Yes on the General Tab.

If you have FireFox with FireBug, or Chrome or IE with the DeBug Bar then it's failry easy to check if the mootools.js (or some variant) is loaded. Note that Joomla 1.5 uses the older 1.1 version of MooTools; many of the add-ons you cna find on the web use MooTools 1.2 which is not expected in Joomla until 1.6.

window.addEvent('domready',function{....}); is a MooTools/JavaScript command to load the JavaScript that is contained in it when the page html has finished loading. If JavaScript loads before that there is a danger that it will look for some DOM element that isn't yet loaded and will fail. (There is a similar 'onLoad' function also in use, but that also waits for all image, scripts, etc. to load and can delay the page being operational.)

I currently like to use 'domready' with some Joomla code to load scripts into the page header (sometimes scripts in the page body can also cause problems). Here's a recent example from my Form HTML
<?php
$script = $style = "";
$script_array = $style_array = array();
$doc =& JFactory::getDocument();

// add a style snippet
$style .= "
. . . some css . . .
";
// add a css file
$style_array[] = JURI::base().'some_path/Autocompleter.css';

// add a script snippet
$script .= "
. . . some javascript  . . .
";

// add a couple of script files 
$script_array[] = JURI::base().'some_path/assets/Observer.js';
$script_array[] = JURI::base().'some_path/Autocompleter.js';
. . .

// load scripts and styles at the end of the page
// add the script snippets if there are any, wrapped in 'domready'
if ( $script ) {
    $script = "window.addEvent('domready', function() { $script });";
    $doc->addScriptDeclaration($script);
}

// add any script files
if ( count($script_array) ) {
    foreach ($script_array as $s) {
        $doc->addScript($s);
    }
}

// add any css sniippets
if ( $style ) {
    $doc->addStyleDeclaration($style);
}

// add any css files
if ( count($style_array) ) {
    foreach ($style_array as $s) {
        $doc->addStylesheet($s);
    }
}
?>
This may look a bit long-winded but it offers a some advantages. All the snippets (css or javscript) are gathered into one block (of each) and loaded into the header. Only the necessary css and JavaScript files are loaded (I can use conditional php to add them to the arrays) and they all go into the header.

That leaves the Availalbility function. What do you have at present as code for this - does it require database searches to execute?

Bob
daffy 16 Apr, 2009
Thanks for that reply Bob. There are many things in it about which I know nothing ! I have not googled JURI yet but I will.
Yes my function to find availability is based on a stored program which has worked for some 6 years on Firebird.

I am pleased to find that stored procs on MySQl are very similar. (Of course Firebird = Interbase was years ahead of MySql but MySQl has caught up well !) I now have to use Firebird, Firefox and Firebug...but still get no Fireworks! Each item for hire such a a left-handed corkscrew has its own diary for the next 200 days in which every booking-day is counted. So if a customer demands 4 lefthanded corkscrews for a week on 5th June the program finds the largest count of booked corkscrews over that week and subtracts it from the stock level so can say whether or not some should be available. Of course the stock level can change if one is lost or broken so another one has to be bought (in a panic) if required.

Now two more issues:
I have implemented a validation to ensure that of the two dates, start of hire and end of hire, the start is not entered as earlier than today and the end is not before the start. That all works, But now I need two variables the Length of hire (which sets the price) and the distance ahead of today before the hire starts which sets the urgency of preparation. I find I am writing the same code again...subtract today from start and subtract start from end. If I could have done this during 'validation' and then included 'length' and 'ahead' into the stored database then I should not have needed to recalculate them (either being negative would act as validation error).But how can I add some extra data in a way such that it (these 'validation' results' can go into the database? ( I don't mind setting up the database to suit.)

Second Issue:
When a validation error arises it would be nicer if the form were a sticky form so that the user could see where he had gone wrong...as it is he just has to fill the whole form in again. Is it easy to make the form sticky ?

Thanks very much for your help. I am studying and learning hard. (Paracetamols are dissappearing rapidly).
--
Dave🙂
GreyHead 16 Apr, 2009
Hi Dave,

To save a value from a JavaScript calculation, set up a hidden input in your form
<input type='hidden' name='length' id='length' value='' />
Then set the value in your JavaScript
$('length').value = some_value;
Should work fine - you can see this in the Ajax zipcode lookup I posted a day or so ago.

If you ask ChronoForms to "Republish fields if wrong submission ?" on the AntiSpam tab it will do it's best (Usually pretty good) to reshow the values - otherwise it's fairly easy, but tedious, to code manually.

Bob
daffy 17 Apr, 2009
Thanks for the reply Bob,
But various books say I must not use Javascript unless I also provide for clients who have turned javascipt off on their browsers. Would you say this was still applicable.?

Because of this I have provided both JSValidation and server side PHP validation. Can I 'merge in' the PHP results so that they go into the database or will it only work with JS ?

Looking at the 'autogenerated code' It looks as if I should be able to 'hack' this to feed another calculated variable in, provided of course that I made an appropriate column in the table. Something like

JRequest::setVar("length",JRequest::getVar("length",global $my_calculated_length,"post","integer",""));


Is this sensible ? Or does it rely on Javascript to function anyway ?

--
Dave
Max_admin 21 Apr, 2009
Hi Dave,

your last PHP code statement looks fine but I don;t think the global word should be where its

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 21 Apr, 2009
Hi Dave,

I'd rewrite the snippet as
<?php
global $my_calculated_length;
$length = JRequest::getInt("length", $my_calculated_length, "post");
JRequest::setVar("length", $length, 'post');
?>


There is no JavaScript in there.

I understand the argument about JavaSCript but in practice today I think it is a vanishingly small proportion of 'techie' users who browse without JavaScript. I'd expect it to be a fraction of 1% of your users. I think the better approach is one of safe degradation - where the code still functions OK-ish in the absence of Javascript.

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