Welcome Guest, Not a member yet? Register   Sign In
Problem with jQuery ajax (not validating data)
#1

[eluser]minoflow[/eluser]
Being somewhat of a CI and jQuery newb, I'm trying to snazz up a little email form with some ajax calls. The problem I keep running into is that my validations and rules do not work. It will send the data just find (I'm getting the emails) but I'm not getting any of the validation errors or feedback which worked before I started adding the jQuery stuff.

Here is the code

Controller
Code:
<?php

class Contact extends Controller {
    
    function Contact()    {
        parent::Controller();
        $this->load->library('validation');        
    }
    
    function index($data = array()) {
        if (isset($_POST)) {
            $rules['name']        = "trim|required|xss_clean";
            $rules['email']        = "trim|required|valid_email";
            $rules['message']    = "trim|required|xss_clean";
            
            $this->validation->set_rules($rules);
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
                
            if ($this->validation->run() == TRUE) {
                $this->load->library('email');
                
                $this->email->from($this->input->post('email'), $this->input->post('name'));
                $this->email->to('[email protected]');
                $this->email->subject('Email from jaygiesen.com');
                $this->email->message($this->input->post('message'));
                
                if (!$this->email->send()) {
                    $data = array('feedback' => 'E-mail failed to send, try emailing me at [email protected]');
                } else {
                    $data = array('feedback' => 'Thank you for your inquiry, I will get back to you as soon as possible.');
                }
            }
        }
        
        $data += array(
               'title' => 'Jay Giesen : Contact',
               'nav' => $this->nav->get_nav_data(),
               'content_view' => 'contact',
        );
        
        $this->load->vars($data);
        $this->load->view('container');
    }
    
}
?&gt;

View - Header with jQuery
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;



    $(document).ready(function() {
            
        // handle the contact form ajax request.
        $("#contact_form").submit(function() {
            var form = $("#contact_form");
            var action = form.attr("action");
            var data = form.serialize();
            $.post(action, data, function() {
                alert("sucess");
            });
            return false;
        });
        
        jQuery().ajaxStart(function() {
            alert("starting");
        });
        
    });

&lt;link rel="stylesheet" href="&lt;?=base_url()?&gt;css/default.css" type="text/css" /&gt;
&lt;/head&gt;

&lt;body&gt;
<a name="top"></a>

View - Form
Code:
<div id="content">
    <p>This is my contact information.</p>
    &lt;?=$this->validation->error_string; ?&gt;
    &lt;?php
    if (isset($feedback)) {
        echo "<div class=\"success\">$feedback</div><p>".anchor('/contact/', 'Send me another email')."</p>";
    } else {
        echo form_open('contact/', array('id' => 'contact_form'));
        echo "<p>";
        echo form_label('Your Name:', 'name');
        echo form_input(array(
                            'name'            => 'name',
                            'id'            => 'name',
                            'maxlength'        => '50',
                            'size'            => '25',
                            'value'            => $this->input->post('name')
                            ));
        echo "</p><p>";
        echo form_label('Your Email:', 'email');
        echo form_input(array(
                            'name'            => 'email',
                            'id'            => 'email',
                            'maxlength'        => '50',
                            'size'            => '25',
                            'value'            => $this->input->post('email')
                            ));
        echo "</p><p>";
        echo form_label('Message:', 'message');
        echo form_textarea(array(
                            'name'            => 'message',
                            'id'            => 'message',
                            'rows'            => '10',
                            'cols'            => '45',
                            'value'            => $this->input->post('message')
                            ));
        echo form_submit('submit', 'Send');
        echo form_close();
    }
    ?&gt;
</div>

I would really appreciate any feedback, these forums have been a great tool for me to develop my CI skills.
#2

[eluser]fdog[/eluser]
I have the same problem. I use a variation of Trevor Davis's ajax form with JQuery and it's been impossible for me to validate using CI. I have to do it in the client's side using javascript which I know it's a bad idea.
#3

[eluser]minoflow[/eluser]
Bummer, I would hate to have to do the validation with javascript. I'd much rather prefer doing it within CI...
#4

[eluser]xwero[/eluser]
Have you found out where it goes wrong because i was able to do ajax validation some time ago.

What happens when you change
Code:
$this->validation->run() == TRUE
to
Code:
$this->validation->run() === TRUE
#5

[eluser]minoflow[/eluser]
No luck
#6

[eluser]xwero[/eluser]
does the form_open function return the relative url or the absolute url?
#7

[eluser]minoflow[/eluser]
it returns the absolute url as action="http://127.0.0.1/~jonathanminori/jaygiesen/index.php/contact"
#8

[eluser]gon[/eluser]
Try using firebug extension for firefox.
It allows you to monitor ajax calls and check visually that all params are ok.
Also, you can right-click on any ajax POST call and open it as if it was a normal POST form.
So you can reload the page and put some var_dumps et al.

Hope it helps.
I don't have the time to check where your error might be, just to give some advice on how to debug ajax.

BTW, I do ajax validations in CI with no problem.

Regards.




Theme © iAndrew 2016 - Forum software by © MyBB