Page 1 of 1

Zend Framework:: jquery ajax contact form:: spam code issue

Posted: Fri Jun 26, 2009 6:07 pm
by jaoudestudios
I have my contact form working without ajax. I can get it almost working fully with jquery ajax however the spam code keeps failing even though it is correct. I have included my actions & jquery below.


The normal contact action is below...(it works, but just included it to be complete - if you do look through it and have improvements, they are welcome)

Code: Select all

public function contactAction()
    {
            # get form
            $contactForm = $this->_getContactForm();
            
            # if there is a post
            if ($this->getRequest()->isPost()) {
                $formData = $this->_request->getPost(); // get post data
                $contactForm->populate($formData);          // populate form with data
                
                # check to see if from a quick form, if so do not display errors just yet
                if (empty($formData['quick']))
                {
                    # valid post and display message to user accordingly
                    if (!$contactForm->isValid($_POST))
                    {
                        $this->view->error = "Please fill in all required information marked in red"; // message to the user
                        
                        # display form with filled in data
                        $this->view->contactForm = $contactForm;
                    }
                    else 
                    {               
                        $this->view->error = "Message sent"; // message to the user
                        
                        # get info from global.ini
                        $contact        = Zend_Registry::getInstance()->global->contact;
                        $emailTo        = $contact->email;
                        $emailName  = $contact->name;
                        
                        # send email
                        $mail = new Zend_Mail();
                        $mail->setFrom($formData['email'], $formData['name']);
                        $mail->addTo($emailTo, $emailName);
                        $mail->setSubject('JS: '.$formData['subject']);
                        $mail->setBodyText($formData['info']);
                        $mail->send();
                    }
                }
                else
                {
                    # display form with filled in data
                    $this->view->contactForm = $contactForm;
                }
            }
            else
            {
                # display form empty
                $this->view->contactForm = $contactForm;
            }
    }
Now i would like to use jquery to ajax this contact, so I have an additional action that wraps the action above...

Code: Select all

public function contactajaxAction()
    {
            # disable display
            $this->_helper->layout->disableLayout();
            
            # contact page
            $this->contactAction();
        }
My jQuery...(works, but including it to be complete)

Code: Select all

// AJAX
    $.fn.ajax = function() {
        $('.ajax').ajaxStart(function() {
            $(this).html('<img src="/images/loading.gif" alt="ajax loading" />');
        });
        $('.ajax').ajaxStop(function() {
            $(this).html('');
        });
    }
    
    // CONTACT FORM
    $.fn.contactForm = function() {
        $('form#contact input#submit').click( function(event)
        {
            event.preventDefault();
            
            // get form data
            var name            = $('form#contact input#name').val();
            var email       = $('form#contact input#email').val();
            var subject     = $('form#contact select#subject').val();
            var info            = $('form#contact textarea#info').val();
            var spamcode_id         = $('form#contact input#spamcode-id').val();
            var spamcode_code   = $('form#contact input#spamcode-input').val();
            
            // send form data & get return data
            $('div#content').load('/index/contactajax/', 
                                                            { 'name'                        :name, 
                                                                'email'                 :email, 
                                                                'subject'               :subject, 
                                                                'info'                  :info, 
                                                                'spamcode_id'       :spamcode_id, 
                                                                'spamcode_code' :spamcode_code },
                                                                function() {
                                                                    $.fn.contactForm(); 
                                                                }
                                                        );
        });
    }
    
    $.fn.contactForm();
    
    $.fn.ajax();
So why does the spam code keep failing? Therefore the email is not being sent. Any ideas?

Re: Zend Framework:: jquery ajax contact form:: spam code issue

Posted: Fri Jun 26, 2009 6:46 pm
by jaoudestudios
Solved it. I was posting the wrong values to the php with my ajax call

Re: Zend Framework:: jquery ajax contact form:: spam code is

Posted: Thu Mar 25, 2010 7:15 am
by elizas
When a form is submitted through submit button click the form data is subsequently cleared or reset . But when we submit a form by AJAX then we have to clear the form data by picking every element and setting the value as blank. If a form contains more than 100 input fields then it is a tedious job to clear or reset the form data.

But jQuery can help you avoid this problem, itoffers many different ways to clear form data more easily. I have written some functions based upon different requirements.

Re: Zend Framework:: jquery ajax contact form:: spam code issue

Posted: Thu Mar 25, 2010 8:22 am
by Eran
When a form is submitted through submit button click the form data is subsequently cleared or reset . But when we submit a form by AJAX then we have to clear the form data by picking every element and setting the value as blank. If a form contains more than 100 input fields then it is a tedious job to clear or reset the form data.
You can simply use the built-in reset function in forms.
https://developer.mozilla.org/en/DOM/form.reset

@jaoudestudios
Use should replace all the tedious value fetching using serialize()
http://api.jquery.com/serialize/