Hack - Add Query string to Redirect URL in Chronoforms

geilt 18 Apr, 2009
I had an instance where I needed an ID Variable to be placed in the URL as a query so i could pull it down from the next page with Javascript.

I wanted the URL Redirect field not to just go to http://www.example.com/thinking.html but to http://www.example.com/thinking.html?xID=(ID Variable)

In order do do this we need to edit chronocontact.php in components/com_chronocontact

On Line 359 we find: $mainframe->redirect($cf_rows[0]->redirecturl);
We concactenate the query string we want and add the POST Variable from the previous form (that i was generating with mktime() )
So we change it to : $mainframe->redirect($cf_rows[0]->redirecturl."?xID=".$_POST['xID']."");

Hope this helps some of you!

Complete Modified Code:

if ( !empty($cf_rows[0]->redirecturl) ) {
if ( !$debug ) {
$mainframe->redirect($cf_rows[0]->redirecturl."?xID=".$_POST['xID']."");
} else {
echo "<div class='debug' >Redirect link set, click to test:<br />
<a href='".$cf_rows[0]->redirecturl."'>".$cf_rows[0]->redirecturl."</a></div>";
}
GreyHead 18 Apr, 2009
Hi geilt,

Nice hack.

You can also do this in the OnSubmit code box I think without hacking the code:
$new_var = JRequest::getVar('xID', '', 'post);
$cf_rows[0]->redirecturl .= "?xID=".$new_var;
?>

Bob
Ludwig 30 Apr, 2009
Hi,

You can also do this in the OnSubmit code box I think without hacking the code


As GreyHead said, not hacking the code is the better way to do things. But I guess the code above does not work. I use the following code to automatically add the Itemid of the form to the redirect link:
<?php
    $MyForm->formrow->redirecturl .= '&Itemid='.JRequest::getInt('Itemid', 0, 'GET');
?>

You can add it to one of the two 'On Submit code' areas.

And this code would help in your case:
<?php
    $MyForm->formrow->redirecturl .= '?xID='.JRequest::getInt('xID', 0, 'POST');
?>

I guess your timestamp is an integer, so using getInt or getWord is more secure than getVar.

Christian
Thunda 18 Mar, 2010
Hey guys - sorry for dragging this up from the past .. but I'm trying to do exactly what is being talked about here (not the code hack - the onSubmit way) - but no matter what I do form won't add the variable into the redirect URL.

As a test - I have displaying in my actual form the code (my variable on the form is $id) :

$MyForm->formrow->redirecturl .= '?locid='.JRequest::getVar('id', 0, 'GET');


This is the only variation would work! The options in previous posts would just return a NULL value for ID. But this code produces the following displayed on my form ..

component/option,com_chronocontact/chronoformname,1_con_dat_new?locid=38


Good so far! I know the JRequest is picking up the variable!

However, when I put the exact same code in the forms onSubmit (either before or after email) within ChronoForms and echo it out, it shows :

component/option,com_chronocontact/chronoformname,1_con_dat_new?locid=0


This has stumped the hell out of me .. when my email form returns from sending the email, I want to insert a record into a table showing that an email has been sent for that record ($id being the record number in my email form) before redirecting .. but, I'm at a loss .. I've tried the plugins in the form .. nigh on everything I can think of .. but I can't grab hold of $id and pass it on once the Submit button has been pressed.

What am I doing wrong?
GreyHead 18 Mar, 2010
Hi Thunda,

The 'get' request works in the Form HTML becasue it picks up the info from the calling URL - but I don't think that is preserved after the form is submitted.

You may need to save it to a hidden form input and then pick it up again from the 'pst' array.

Also the ? syntax is only correct if this is the first parameter in the query string, you need & for the other parameters.

Bob
Thunda 18 Mar, 2010
Hey GreyHead .. The "?" was probably from the last combination of efforts on my part!

I think this might be a case of not seeing the wood for the trees on my part .. Heres the flow of what I'm doing.

I have a ChronoForm showing a list of locations (plus can edit locations - hence why its a form) - this form is called : index.php?option=com_chronocontact&chronoformname=1_con_dat_new

On this form is an "email" link - which opens a popup with an ChronoForm Email form on it .. this form is called : index.php?option=com_chronocontact&chronoformname=ngi_admin_email

After pressing Submit (and sending the email) the ngi_admin_email form returns to the 1_con_dat_new form in the pop up window (this redirect is in the admin section of ngi_admin_email within ChronoForms component).

I already pass one parameter back with it ... ?action=closewindow ... which when it loads 1_con_dat_new in the popup it closes the window (the code for this is at the beginning of my 1_con_dat_new form and acts like an event checking for action=closewindow) - it is here that I also want to capture the ID of the record of the location thats just been emailed and write a log of it to a mySQL table before the popup closes.

Before the form is displayed I make this check on 1_con_dat_new form



$action=$_GET["action"];
$id=$_GET["id"];

if ($action=="closewindow")
{
// ****** write my code to Insert "email sent" information into table
// ****** but need to capture $id or Hidden Field from recently ngi_admin_email form
//
// ****** My SQL stuff will go here
// 
// ******

// **** display message to user and then close the pop up
echo "You're email has sent and the database has been updated - this window will now close";

?>
<script language="javascript"> 
<!-- 
window.resizeTo(200, 200);
window.moveTo(500,300);
    
setTimeout("self.close();",5000) 
//--> 
</script> 
<?

exit;
}


In the ChronoForm component I have the following information for ngi_admin_email

redirect url : index.php?option=com_chronocontact&chronoformname=1_con_dat_new?action=closewindow

This works fine. I would like the redirect to be equal to the following :

Desired URL : index.php?option=com_chronocontact&chronoformname=1_con_dat_new?action=closewindow&id=[whatever the id was on the email form]

I have under onSubmit in the ChronoForm component for ngi_admin_email :

<?
$MyForm->formrow->redirecturl .= '&id='.JRequest::getInt('id', 0, 'GET');
?>


(I've tried every combination of POST and GET!!)

But instead of the browser window opening : index.php?option=com_chronocontact&chronoformname=1_con_dat_new?action=closewindow&id=[whatever the id was on the email form]

It ALWAYS shows :

index.php?option=com_chronocontact&chronoformname=1_con_dat_new?action=closewindow&id=0

Help!!!

Thanks for reading - hope that makes sense!
GreyHead 19 Mar, 2010
Hi Thunda,

Early morning and two cups of coffee and I think I understand this.

I think that you should capture and save the OnSubmit part of ngi_admin_email. This would short-cut the problem that you are having. You can add the MySQL to do the update in the OnSubmit After box.

Bob
Thunda 20 Mar, 2010
Hey Bob - thanks for that. That seems to have done the job - but I'm sure I'd tried that way before! Ah well - all sorted now! Thanks
awilkin 21 Aug, 2010
I have a slightly different twist, am trying to achieve a number of different things, hopefully this post will explain sufficiently.

Page 1 (with embedded form using the plugin):
* Captures name
* Captures email
* Captures phone number
* Adds to an auto responder (using Curl)
* Redirects to page 2

Page 2 (with embedded form):
* Thank you message
* Captures address if they want an information pack
* Check box to add email newsletter options for auto responder on page 1

I did look at the multi form functionality, but want things embedded in another article and this does not seem to work (correct me if I'm wrong here).

I also want to ensure that I capture information from page 1 before they see page 2 in case they decide to abandon at that stage.

I need to be able to associate the data submitted on page 1 with the data submitted on page 2, but being fussy I don't want to do this by adding to the URL (people probably would not want this associated with privacy and all that).

Any ideas here?

Thanks,

Andy
GreyHead 22 Aug, 2010
Hi Andy,

Building a multi-step form that will embed in an article is tricky. ChronoForms usually doesn't have enough control over the site URLs to do that reliably.

You could do it with an Ajax form re-writing the results inside the page I think. Possibly it could be done by putting the form inside a module on the page?

Passing the information is relatively trivial compared with managing the display in this case.

Bob

PS The book does have an example of a form in a lightbox that might do the trick?
NathanSmith 05 Jan, 2011
Sorry to bring up an older thread but I have a similar problem but with a twist.

Instead of adding an ID variable at the end of the redirected URL, I am wanting to dynamically add a 'directory' before the redirected URL.

I'll try to explain:

I am running a multi-lingual site with JoomFish as the main language handler(I know, I know, you don't support JoomFish, but hear me out please.)

I have it set up so that ChronoForm works and sends to separate e-mails and email templates depending on what language the person is using. All great. Except one thing.

When the Form is filled out, and the user hits the Submit button, it goes to a 'Thank You' page. This thank you page is set to the default language of the site(en-US). No matter if they were using the German version of the site, once they submit the form the site reverts back to English and if they browse the rest of the site, it will be in English - not German.

I actually found a Pseudo-Fix for this problem:

I created an Article called Thank You and a menu item pointing to it with the alias of 'thank-you'
In the General Tab in the 'Redirect URL' box I put 'thank-you' so that after hitting the Submit button, users would be redirected to the article. However, I still got the problem with it defaulting back to the English setting. So I tried something else in the Redirect box:

I put 'de/thank-you' in the Redirect URL box and when I hit submit, it defaulted to the German area of the site. This tells me that just having the language identifier right before the 'thank-you' alias can help determine behavior of the Thank You page.

So what I am asking is, where and how might I be able to edit the Redirect URL box functionality so that it will dynamically see what language the user is in and inserting that short language code right before it calls whatever I put in the Redirect URL box.

That make sense? Or is that taking the long way? I'm not sure if I can redirect via PHP after the OnSubmit, because by that time ChronoForms has already gone back to English as default and if it tries to redirect then, it will decide it is English all the time (i think? I could be wrong about this behavior).
NathanSmith 29 Mar, 2011
Okay, so a couple months later and I figured out a solution to my JoomFish problem.

This has been plaguing me for months, and I just recently made a breakthrough and fixed it. I had to hack a primary Chronoform file to do it. I wanted to document this somewhere and also try and help other people who might have had the same problem as me. So here it is.

Environment
Joomla 1.5.20
JoomFish 2.1.5
ChronoForms 3.0

Problem
Every time a user would fill out a form created by Chronoform, after submission the site would revert back to the default language. Example: If I browse the site in German, and then fill out a Form and hit 'Submit', I would then be viewing the site in English, despite the fact I was viewing German before submission.

Solution
2 main edits to components/com_chronocontact/libraries/chronoform.php

1st edit:
Original Line 195
$actionurl = $CF_PATH.'index.php?option=com_chronocontact&task=send&chronoformname='.$MyForm->formrow->name;


Fixed Line 195
$actionurl = sefRelToAbs('index.php?option=com_chronocontact&task=send&chronoformname='.$MyForm->formrow->name);


2nd edit:
Original Line 490
$mainframe->redirect($MyForm->formrow->redirecturl);


Fixed Line 490
$mainframe->redirect(sefRelToAbs('index.php?Itemid='.$MyForm->formrow->redirecturl));


How this Changes The Way You Use ChronoForm
You'll see on line 490 one of the things I added was 'index.php?Itemid='. What this is essentially doing, is changing the way the 'Redirect URL' field in Chronoforms works.

With this fix, you now put in the Menu Itemid of any page you want the form to redirect to. Example: I put in the number "570" (without quotes) into the 'Redirect URL' field in the General Settings of my ChronoForm. This means that anytime someone fills out that specific Chronoform, it will redirect them to http://www.example.com/index.php?Itemid=570 . With JoomFish and SEF enabled, this is actually a dynamic URL and will show up as whatever SEF Alias you set for Menu Item.

And don't worry, even though I also edited the $actionurl to dynamically include the SEF urls, Chronoforms still writes to the database for submission logs, if you have it enabled. I had to edit the $actionurl because that was where Chronoforms was dropping the language variable. It uses $CF_PATH which seems like it is hardcoded to be default. With this change, it is more dynamic and the path is instead changed to include the language variable so that it can in turn be used during the redirect URL edit.

At least, that was my logic in changing everything.

Problems With This Hack
Obviously, this is a hardcoded hack, which is never the best solution. Perhaps a plugin and maybe a content element would be better fix so it could be more dynamic. Also if you update chronoforms, this hack will disappear.

Also, I know sefReltoAbs is an old function, so it might be necessary to have the Legacy mode plugin enabled for this to work. I haven't tested this though, because Legacy mode has always been on for me.

Only other problem I can find with it is that if you don't dictate a redirect URL (for example, you just use the OnSubmit or AfterSubmit field to put in a Thank You message) it makes submission a lot slower. It still submits, and it still keeps the language variable. It's just much slower in doing so. Not quite sure why this is. Maybe I forgot to close something and it's running more loops than it has too.


This fix, combined with the Chronoforms Content Elements for Joom!Fish allows my to create only ONE form, translate it, and have it go to my redirect URL and still keep the language variable. All without having to mess with long and tedious switches and 'if' statements for every single form I create.

I would like you comments on this hack, as I am not a real programmer. While everything looks to be working 100% perfectly, I would like to know if making these changes is perhaps hurting me in some unknown area.
GreyHead 29 Mar, 2011
Hi NathanSmith,

This looks like a practical fix for your problem. Many sites don't use Menu IDs in quite that way so it's not a general purpose fix.

I'm not clear why the problem arises, not something I've ever encountered using ChronoForms with JoomFish - though that's only been a handful of sites. Usually it seems the language gets picked up correctly from the Language object and doesn't need to be in the URL.

Bob
NathanSmith 30 Mar, 2011
I posted this on the JoomFish Club Support forums too. They seem to think that it wasn't passing the language variable because I disabled the ability to store the language as a cookie.

I turned off the cookie cache for JoomFish because it was causing some annoy troubles for my translators, who live around the world. Some translate into multiple languages, and the cookie cache was preventing them for easily viewing the site in any language without hindrance.

So it looks like this might be a possible fix if you don't want to store the JoomFish language as a cookie on the viewers computer.
vashanka 06 Jun, 2011
I have a similar (probably dumb) question and am hoping for some help. Specifically, I need to append my form field values to the redirected URL (the 3rd party system we're submitting to requires values passed through query string).

So, I need the final submitted URL to look something like:

http://their.formhandler.com/aspx?FormName=myform&SiteID=123&email=my.email@email.com&FirstName=Ima&LastName=Test

etc. All values come from the form fields and a couple of hidden fields.

I'm thinking I can modify the code snippet below and place it in ChronoForms OnSubmit Code field:

<?php
    $MyForm->formrow->redirecturl .= '?xID='.JRequest::getInt('xID', 0, 'POST');
?>


Any help is greatly appreciated, I'm not really a PHP guy.
GreyHead 07 Jun, 2011
Hi vashanka ,

The ReDirect Plugin is probably the easier way to do this.

I think your code will work but it may need to be &xID= in place of ?xID= depending on the saved redirect url.

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