Modifying AJAX Form code

Permalink Browser Info Environment
If I want to make modifications to some of the code the the AJAX Form block... what's the best way to go about it? Should I just make changes directly to the files in the /packages/ajax_form directory? Or should I copy them somewhere else and rename things?

I'm used to dealing with Block overrides... but these packages have me a bit confused (especially since I want to modify the file in the TOOLS directory of the Ajax_Form package.

Thanks!

- John

Type: Discussion
Status: New
arrestingdevelopment
View Replies: View Best Answer
jordanlev replied on at Permalink Reply
jordanlev
You shouldn't need to change anything in the tools file (although I can't say this for sure -- it depends on what exactly you want to do). I set up the template so that you should be able to modify the javascript easily from the template itself -- so copy SITEROOT/packages/ajax_form/blocks/form/templates/ajax_tableless_layout.php to SITEROOT/blocks/form/templates/ajax_tableless_layout.php (or you could rename it after copying if you want).

Near the top of the template file you'll see some javascript code (between the <?php if ($isAjax): ?> and <?php endif; ?>). There are 3 functions that get called after certain things happen (see code comments for explanation of what each one does). So as long as what you want to do is "something after form failure" or "something after form success", then you should be good.

If you can provide specific javascript code (or an explanation of what you want to happen step-by-step), I can try to help a little more.

Good luck.

-Jordan
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Hey Jordan,

Thanks! I apologize... I actually forget WHAT it was I was thinking of when I posted this. I think I actually mis-typed, and meant to ask about how to do it so that I could edit the "form_template_header.php" file in the "elements" folder. But, to be honest, I don't remember WHY I wanted to do that! You're right... it looks like everything I needed can be handled from the template files.

Sorry 'bout that!

- John
arrestingdevelopment replied on at Permalink Reply 1 Attachment
arrestingdevelopment
Hi Jordan,

OK... NOW I remember what it was I wanted to change! Here goes:

I've modified the "ajax_tableless_layout.php" file because I wanted to have any errors for the form displayed in an alert box, instead of appearing in the form as a DIV (the form is in a small area and doesn't have room to accommodate the extra text). The problem was that the alert box then shows the HTML DIV tags, etc, because the errors have been pre-wrapped in divs. It looks like this (see attached image, too):

Please correct the following errors:<div class="error">Please complete all fields on the form.</div>


So I was thinking I needed to edit the form_template_header.php file around line 81 in order to NOT insert the divs, like this:

$errorDivs .= $error;


But maybe there's a better way that can be handled right from the template file?

I appreciate your help!

- John
jordanlev replied on at Permalink Reply
jordanlev
Hmm... well if it's just the divs that you don't want showing, it might be best to figure out a way to *not* output the html as text. In other words, it looks like some code somewhere is escaping the error message because the html is being outputted as the actual code tags. So if you figure out how to render html as html, but then make it so the style of that div is no different than any other text around it, it would then basically become invisible.

Otherwise, yeah you'd have to modify that other file to change the output of the error messages.

PS: I never received a notification about your previous post -- seems to be some problems with the forums right now, so if you have other questions and don't get a response from me within 24 hours or so, feel free to PM me or email me directly at concrete@jordanlev.com
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Jordan,

No problem on not having received the earlier message... I was (at the time) abandoning the whole idea and (as you know) was looking for someone else to code it. Luckily (?!?!) the client has changed the requirement for the form processing, so now it's more within my realm of "expertise" (<-- term used VERY loosely, LOL!).

On the alert problem... I'm using a javascript alert(); function, so that's why any HTML code is being displayed. :(

In my modified "ajax_tableless_layout.php", I changed linkes 22-27 from:
function <?php  echo $template_onerror_funcname; ?>(form, errors) {
   //This js code happens after form is rejected due to validation errors...
   $(form).find('div.errors').html(errors).show();
   $(form).find('div.indicator').hide();
   $(form).find('input.submit').show();
}


to this:
function <?php  echo $template_onerror_funcname; ?>(form, errors) {
   //This js code happens after form is rejected due to validation errors...
   alert(errors);
   $(form).find('div.indicator').hide();
   $(form).find('input.submit').show();
}


It would appear that the errors are being wrapped in the <div class="error"> code in the "ajax_responder.php" file from within the TOOLS folder of the package... at line 54 is this:

foreach ($errors as $error) {
   $response .= '<div class="error">'.$error."</div>";
}


So I guess I'm back to having to edit the core code of the Ajax Form package in order to prevent the divs from being wrapped around the error.

Unless there's some way to duplicate and rename the package? If I copy the package and rename it, I'm assuming that means I would have to parse through all of the files (controller.php, elements/form_template_header.php, tools/ajax_responder.php and the blocks/form/templates files) in order to change any package-specific references, right?

Thanks!

- John
jordanlev replied on at Permalink Best Answer Reply
jordanlev
I wouldn't try duplicating the whole package -- seems like a lot of work for no reason. Probably just changing the tools file is the best approach. Although, you might want to try adding some javascript in the template to just remove the div substrings from the error message, like so:
errors = errors.replace('<div class="error">', '');
errors = errors.replace('</div>', '');


You may need to play around with those find/replace strings or use a regex instead, but that's the basic idea.
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
That's GENIUS! Thanks... I'll give the JavaScript approach a shot! I had such blinders on, only thinking about making the mod in the PHP!

Thanks!

- John
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Worked like a charm!!!

Thanks, so much, Jordan! You ROCK!

- John
jordanlev replied on at Permalink Reply
jordanlev
You're very welcome -- glad it finally got figured out (and without you needing to hire someone).
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
LOL! Mostly due to the fact that the client changed their mind and decided NOT to have an automatic confirmation, with links to the PDF brochures, go to the form submitter and, instead, do it the old-fashioned, manual way for greater personalization. If they hadn't changed that, I wouldn't have had a prayer.

Oh, yeah... and I couldn't have gotten it to work without your awesome code and generous assistance! You should add a PayPal " donate" button to your profile... 'cuz I'm sure I'm not the only one wo would use it to thank you!!!

REALLY appreciate your help, Jordan!

- John

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.