Forums

CF Form Design Question: Lookup Value and Redirect to URL

6ds 05 Jun, 2011
Hello, longtime user for basic forms. Best Joomla! form component!

I am needing to design a form that will take an email address input by the visitor, split the domain address into a variable, and lookup that string in a text file or other location (doesn't matter), and then based on the result, redirect the visitor to another URL on the same page.

The purpose of this is for our customers to enter their email address, and be redirected to the correct login/information page based on their email server. Example data:

domain1.com >> server1.mail.com
domain2.com >> server2.mail.com
domain3.com >> server1.mail.com

Here is the pseudo that I've worked out so far:
$em="mynam@domain.com";
$ar=split("@",$em);
$search = 'whatever';
foreach (glob($dir . '/servers.txt') as $file) {
    if match::server1 {
        header (location: $document->baseurl/index.php?option=com_content&task=view&id=XX&Itemid=YY
        break;
    } elseif match::server2 {
header (location: $document->baseurl/index.php?option=com_content&task=view&id=XX&Itemid=YY
    } else {
        echo "Could not find account information. Please check your entry.";
    }
}


But I'm really lost and this is a bit above my skill level. Any help would be appreciated.🙂
GreyHead 06 Jun, 2011
Hi 6DS,

Your pseudocode is pretty close. Let's assume that you have your look-up data in a database table (created with a little ChronoForm of course) including the article and menu ids.
<?php
$em = JRequest::getString('email', '', 'post');
$dom = stristr($em, '@');
$dom = substr($dom, 1);
$db =& JFactory::getDBO();
$query = "
  SELECT `domain`, `server`, `art_id`, `menu_id`
    FROM `#__servers`
    WHERE `domain` = '{$dom}' ;
";
$db->setQuery($query);
$d = $db->loadObject();
$mainframe->redirect(JURI::base().'index.php?option=com_content&task=view&id='.$d->art_id.'&Itemid='.$d->menu_id);
?>

I don't see where you include the server info in here but hopefully there' enough for you to work with.

Bob
6ds 19 Jun, 2011
Thanks!

I'm working on it now, using v4. Had to build out the rest of the site first. I suppose I'll need to build another form for the admins to populate the table when a new customer is enrolled.
6ds 27 Jun, 2011
I get the following error:

Call to a member function redirect() on a non-object

When submitting the form. What does this mean?
GreyHead 27 Jun, 2011
Hi 6DS,

What is the complete error message? The line numbers will help to pin down exactly what the problem is.

Bob
6ds 27 Jun, 2011
Fatal error: Call to a member function redirect() on a non-object in /administrator/components/com_chronoforms/form_actions/custom_code/custom_code.php(17) : eval()'d code on line 9
GreyHead 27 Jun, 2011
Hi 6DS,

That's line 9 of the code in your Custom Code action. What do you have in there?

Bob
6ds 27 Jun, 2011
Nothing. It's just the default.

class CfactionCustomCode{
	var $formname;
	var $formid;
	var $details = array('title' => 'Custom Code', 'tooltip' => 'Run some custom code.');
	function run($form, $actiondata){
		$params = new JParameter($actiondata->params);
		if($params->get('mode', 'controller') == 'controller'){
			$message = $actiondata->content1;
			eval('?>'.$message);
		}
	}


The custom code for the form is what you posted above. Looks like it's having a problem with the redirect.
GreyHead 27 Jun, 2011
Hi 6DS,

No, I want to know if there is any code set up in the action itself. Click the spanner icon to open the Custom Code action configuration and see if there is anything in the Code box please.

Bob
6ds 27 Jun, 2011
Oh, sorry for the misunderstanding. It is the code you provided above, slightly modified:

<?php
$em = JRequest::getString('email', '', 'post');
$dom = stristr($em, '@');
$dom = substr($dom, 1);
$db =& JFactory::getDBO();
$query = "SELECT 'domain', 'art_id', 'menu_id' FROM '#__chronoforms_data_HostedEmail' WHERE 'domain' = '{$dom}';";
$db->setQuery($query);
$d = $db->loadObject();
$mainframe->redirect(JURI::base().'index.php?option=com_content&task=view&id='.$d->art_id.'&Itemid='.$d->menu_id);
?>
GreyHead 27 Jun, 2011
Hi 6DS,

You need backticks `` rather than single quotes '' round the column names in the query. I think the query is failing and so $d->xxx doesn't exist which gives the error.
$query = "
  SELECT `domain`, `art_id`, `menu_id` 
  		FROM `#__chronoforms_data_HostedEmail` 
		WHERE `domain` = '{$dom}';
";

Bob
6ds 27 Jun, 2011
Yes I did try it with the backticks first and it is still not working.

I have the form published here.
GreyHead 28 Jun, 2011
Hi 6DS,

I suspect that the code needs to check if there is a record found and only redirect is there is one.

Bob
6ds 28 Jun, 2011
I have data in the table that should be working. I suppose I should just turn on debug and go from there.

At least I got the form for database save working.🙂 But I am concerned about the security of it.
6ds 04 Jul, 2011
Hello, I needed to initialize $mainframe using $mainframe = JFactory::getApplication();
This topic is locked and no more replies can be posted.