Forums

Redirect to custom URL after submit

cre8tivemedia 10 Mar, 2012
Is it possible to redirect to a url based on the users input into a form? My form allows the user to enter 3 different values into the form which on submit writes directly to my product database creating a new product. This automatically creates a new product page and I would like to have the form redirect to the new page.

Example: User enters Length - 25.25, Width - 15.50 and Depth - 5.75

The created page would be -
/index.php/stock-boxes/rsc/P-rsc-25-1-4-x-15-1-2-x-5-3-4

I can use JS to generate the "P-rsc-25-1-4-x-15-1-2-x-5-3-4" just don't know what actions I need to implement in Chronoforms

thanks,
Matt
GreyHead 11 Mar, 2012
Hi Matt,

You can use the ReDirect URL and ReDirect User actions to do this. use the ReDirect URL action to build the URl and then the ReDirect User action to redirect the user.

Bob
cre8tivemedia 12 Mar, 2012
Thanks Bob,

I can get the redirect to work using the target url. Not quite sure how to append the url with the custom generated "P-rsc-25-1-4-x-15-1-2-x-5-3-4" based on the users input.

Do I need to add a redirect function in the custom code area? or can I just use a tag from the form like "product_url" in the "Params/Fields map" area? Tried looking at other peoples examples but I got kinda lost....

Matt
GreyHead 12 Mar, 2012
Hi Matt,

Build the string in a Custom Code action, save it to a new entry in the $form->data array and then use the name of the new entry in the ReDirect URL Params/Fields box.

Bob
cre8tivemedia 13 Mar, 2012
Hi again,

Hate to keep bothering you over this but I'm stuck. Been trying different things in the ReDirect URL Params/Fields box but just have no idea what I'm supposed to put in there...

I created a field called "product_url" which generates - "P-rsc-25-1-4-x-15-1-2-x-5-3-4"

I put the following in the ReDirect URL's Target URL -

http://example.com/index.php/stock-boxes/pads-dividers/483/199/pads-dividers/



And this is what I put in the ReDirect URL's Params/Fields box:

redirect={product_url}



I am most certain that the Params/Fields is not correct and I keep trying a bunch of different things. Tried putting {product_url} by itself but to no avail, I just get a 404 error...

This is the resulting URL when I process the form:

http://example.com/index.php/stock-boxes/pads-dividers/483/199/pads-dividers/?redirect=P-rsc-25-1-4-x-15-1-2-x-5-3-4



I can see the "product_url" coming through correctly but the "?redirect=" is where this is falling apart. Is there documentation anywhere to show some examples of the Params/Fields options?

Thanks again,
Matt
GreyHead 13 Mar, 2012
Hi Matt,

Sorry, I thought that you wanted a 'normal' query string which is what the ReDirect URL action builds. The fancy versions are usually reinterpreted as SEO URLs.

If you have built your string then you can add this in the same Custom Code action
<?php 
// . . .your existing code
$form->data['redirect_url'] = JURI::root().'/index.php/stock-boxes/pads-dividers/483/199/pads-dividers/'.$form->data['product_url'];
?>
Then use the Redirect URL action when you want to redirect.

Or if you want to redirect as soon as you've built the URL you can use.
<?php 
// . . .your existing code
$mainframe->redirect(JURI::root().'/index.php/stock-boxes/pads-dividers/483/199/pads-dividers/'.$form->data['product_url']);
?>

Bob
cre8tivemedia 16 Mar, 2012
Thanks Bob that worked perfectly. I used the "$mainframe" version.

Ran into another slight problem though...
I didn't realize that in the URL:

'/index.php/stock-boxes/pads-dividers/483/199/pads-dividers/'.$form->data['product_url']


the #483 is the product id ("product_id" in the database) which is set as an auto-increment. I will never know this number without doing an sql query because this could be an existing product or a new one depending on whether it has already been created in the past.

I'm using a misc_id field that is created based on the user input and if it matches in the database then I need to find the product_id that corresponds with it. If it is new then I need to find the newly created product_id

I think I figured out the php to get the value:


<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_database", $con);

$sql = "SELECT product_id FROM product_category_xref WHERE misc_id='$mid'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo $row["product_id"]; 

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con)
?> 


Can I add this to the form somehow or is there another way to load this to be able to have the redirect look something like this:

'/index.php/stock-boxes/pads-dividers/'.$form->data['product_id']'/199/pads-dividers/'.$form->data['product_url']


Thanks,
Matt
GreyHead 17 Mar, 2012
Hi cre8tivemedia,

You can use the DB Record Loader action to load a record from the DB. It will work OK in the On Submit event.

Bob
cre8tivemedia 21 Mar, 2012
Thanks again Bob!

Got it to work but it took me a few days to figure out... I was gonna ask you again what I needed to do after much frustration but I was determined to figure it out on my own. For anyone following this thread here is what I ended up putting in the DB Record Loader:

DB Field: misc_id
Table: mytable
Request Param: misc_id
Load Fields: Yes
Curly Replacer: Yes
Model ID: (left blank)
Load Under Model ID: No
Where Statement: (left blank)
Array Fields Sets: (left blank)
Array Separators: (left blank)

and in my form I added a hidden field called {product_id} with the curly brackets.

Everything is working the way it is supposed to now. Once again I need to thank Bob for his help!
Matt
GreyHead 22 Mar, 2012
Hi Matt,

Well done, and thanks for posting the result.

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