Hi there all,
I have a conundrum in CakePHP. Not hard to do for a newbie like me.
I am trying to do a simple "Send the page to a Friend" form so I can
use it anywhere on the site. The form is loaded via AJAX in a "pop-up"
layer. I thought I could achieve this by just grabbing the URL for the
current page and pass it to a view with a form and have the URL get
set as a hidden input. This hidden input would then get sent with the
email addresses and message, etc. It seemed the easiest way would be
to create a table-less Model and Controller that handled this type of
contact whose sole function was to send of an email. Well, I found out
that passing full URLS to a controller function wasn't so easy, but I
did figure out how to grab all the passed arguments and use them in
the controller to form the URL. This is working fine when the "send"
form first loads, but these passed arguments get lost if validation
fails and form reloads. I just can't think of how to solve this.
Here is my code

Model:
class Contact extends AppModel {
                var $name = 'Contact';
                var $useTable = false;

                var $_schema = array(
                                                'sendUrl' => 
array('type'=>'string', 'length'=>200),
                        'to' => array('type'=>'string', 'length'=>150),
                        'from' => array('type'=>'string', 'length'=>150),
                        'from_name' => array('type'=>'string', 'length'=>100),
                        'message'       =>array('type'=>'text')
                    );

                var $validate = array(
                                        //'sendUrl' => VALID_NOT_EMPTY,
                                        'to' => array('rule' => array('email', 
true), 'message' =>
'Please enter a valid email address.'),
                                        'from' => array('rule' => 
array('email', true), 'message' =>
'Please enter a valid email address.'),
                                        'from_name' => 
array('rule'=>array('minLength', 1),
'message'=>'Your name is required' )
                                );

        }


Controller:
function send_page() {
                        $this->layout = "contacts";
            if (!empty($this->data)) {
                                        $this->data['Contact']['sendUrl'] = 
urlencode("http://
www.pacificfoods.com/".join($this->passedArgs, '/'));
                $this->Contact->set($this->data);
                if ($this->Contact->validates()) {
                                                if(     
$this->_sendPageMail($this->data) ) {
                                                        
$this->Session->setFlash('The page url was sent successfully.
Thanks for spreading the word.', true);
                                                } else {
                                                        
$this->Session->setFlash('Sorry, there was an error trying to
send the email. Please try again later.', true);
                                                }
                } else {
                                                
$this->data['Contact']['sendUrl'] = urlencode("http://
www.mydomain.com/".join($this->passedArgs, '/'));
                                                $this->render('send_page', 
'ajax');
                                        }
            } else {
                                $this->data['Contact']['sendUrl'] = 
urlencode("http://
www.mydomain.com/".join($this->passedArgs, '/'));
                                $this->render('send_page', 'ajax');
                        }
                }

View:
                <?php echo $session->flash() ?>
        <?php echo $form->create('Contact', array('url' => '/contacts/
send_page', 'id' => 'sendpage-form')) ?>
        <?php
                echo    $form->hidden('sendUrl'),
                                        $form->input('to', array('label' => 
"Friend&rsquo;s
Email",'between' => '<br />', 'tabindex' => '2')),
                                        $form->input('from', array('label' => 
'Your Email', 'between' =>
'<br />', 'tabindex' => '3')),
                                        $form->input('from_name', array('label' 
=> 'Your Name', 'between'
=> '<br />', 'tabindex' => '4')),
                                        $form->input('message', array(
                                                'type' => 'textarea',
                                                'between' => '<br />',
                                                'tabindex' => '5',
                                                'label' => 'Message'));
                echo $html->link('close', 'javascript:void(0)', array('id' =>
'formclose','class' => "jqmClose"));
                echo $form->submit('SEND');
                echo $form->end();
?>

Link calling the view:
<?php
        $pgurl  = Router::url("", false);
        echo $html->link('Send this Page',
                                                                                
                '/contacts/send_page/'.$pgurl,
                                                                                
                array('id' => "sendthispage", 'title' => 'Click to email
someone this page'))
?>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to