Is that even close?"> Conditional redirect - Forums

Forums

Conditional redirect

gtownwebdev 11 Jan, 2010
I've seen references to conditional redirects on the forums but can't figure out what the actual code would be. The form has:

<input value="Save this Member and Add Another" name="button_1" type="submit" />
<input value="Save this Member and Finish" name="button_2" type="submit" />

In both cases, I want the e-mail to go out and the data saved to the database, but the first case should reload the form again, and the second case should follow the redirect already specified in the form settings.

I don't know PHP well ... I'm thinking I just a need a couple lines in the OnSubmit area, something like:

<?php
if( trim($MyForm->formparams('submit')) == 'button_1') {
$MyForm->setFormData('redirecturl', "formurlhere”);
}
?>

Is that even close?
GreyHead 11 Jan, 2010
Hi gtownwebdev ,

I've just been writing a recipe for the book to show a conditional redirect. Here's the code snippet I built. It's an odd example but should adapt to your case OK - the last line is the one that you were looking for:
<?php
// get the value of 'name'
$name = JRequest::getString('name', '', 'post');
$name = strtolower($name);
// set the value of $id depending on the 'name' value
switch ($name) {
  case 'john':
    $id = 19;
    break;
  case 'jenny':
    $id = 3;
    break;
  default:
    $id = '';
    break;
}
// create the redirect url 
if ( $id ) {
  $url = 'index.php?option=com_content&view=article&id='.$id;
} else {
  $url = 'index.php';
}
// set the ChronoForms ReDirect URL to the new value
$MyForm->formrow->redirecturl = $url;
?>

Bob
gtownwebdev 12 Jan, 2010
Yup, that last line helps! But how do I grab the name of the submit button (button_1 vs. button_2)? I tried to get the value instead of the name, but it's a long string with spaces ("Save this Member and Add Another") so I don't think it's working. I don't think I need the switch either since it's just either/or (two choices), right? Just

if (somehow grab the name of the first submit button)
$url = 'my-new-redirect-url';
} else {
$url = 'the-regular-redirect-url';
}
// set the ChronoForms ReDirect URL to the new value
$MyForm->formrow->redirecturl = $url;

I don't know what the if-statement should say.
GreyHead 12 Jan, 2010
Hi gtownwebdev,

I'd keep the name of both submit buttons the same - and then check the value. Only one of them will ever be clicked so there is no problem with duplicated names.
<input value="Save this Member and Add Another" name="submit" type="submit" />
<input value="Save this Member and Finish" name="submit" type="submit" />

<?php
$submit = JRequest::getString('submit', 'Save this Member and Finish', 'post');
if ( $submit == 'Save this Member and Add Another' ) {
  . . .
} else {
  . . .
}
. . .
?>

Bob
gtownwebdev 12 Jan, 2010
Works beautifully, thank you!
dudley 16 Aug, 2011
Hello
I have a problem with the following code:
<?php
// get the value of 'name'
$name = JRequest::getString('select5', '', 'post');
$name = strtolower($name);
// set the value of $id depending on the 'name' value
if ( $name == 'yes' ){
    $id = 503;
} else {
    $id = 502;
}
// create the redirect url 
if ( $id ) {
  $url = 'index.php?option=com_content&view=article&id='.$id;
} else {
  $url = 'index.php';
}
// set the ChronoForms ReDirect URL to the new value
$full->formrow->redirecturl = $url;
?>

Trying to redirect the form based on the value of a select box 'select5' in my form 'full'
The code is being ignored. Displaying a blank content page.
GreyHead 16 Aug, 2011
Hi dudley,

Where you have $full-> it needs to be literally $MyForm->

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