Welcome Guest, Not a member yet? Register   Sign In
ajax validation in CI with 'jQuery validation plugin'
#1

[eluser]santorini[/eluser]
The client-side validation is working properly.
The validation using ajax request (remote) does not work.

the path for the request is correct..
the validation methods themselves should be correct.

firebug error:
"NetworkError: 500 Internal Server Error - http://localhost:8888/work/ci_buchverwal...zer_exists"

..but the path to the method 'register_benutzer_exists' is correct..

I don't understand what is wrong..any help?

//view
Code:
echo '<!-- Start Formular -->';
    $attributes = array('class' => '', 'id' => 'commentForm');
    echo form_open('', $attributes);
    echo form_fieldset($label) . PHP_EOL;
    echo form_error('benutzername') ? '<div ' . $fehler . '>' . PHP_EOL : '<div>' . PHP_EOL;
    echo form_label('Benutzername*:', 'benutzername') . PHP_EOL;
    $data = array('name'           => 'benutzername',
                  'id'             => 'benutzername',);
    echo form_input($data, set_value('benutzername', $benutzername)) . PHP_EOL;
    echo '</div>' . PHP_EOL;
    echo form_fieldset_close() . PHP_EOL;
    echo form_fieldset() . PHP_EOL;
    echo form_button('abbrechen', 'zurück', 'onClick=self.location.href="' . $back_link . '"') . PHP_EOL;
    echo form_submit('submit', $submit_text, 'class="mittel"') . PHP_EOL;
    echo form_fieldset_close() . PHP_EOL;
    echo form_close() . PHP_EOL;

//js
Code:
[removed]
    jQuery(document).ready(function ($) {
        $("#commentForm").validate({
            rules:{
                benutzername:{
                    required:true,
                    minlength:2,
                    remote:{
                        url:"&lt;?php echo site_url('redaktion/nutzer/register_benutzer_exists'); ?&gt;",
                        type:"post",
                        data:{
                            benutzername:function () {
                                return $("#benutzername").val();
                            }
                        }
                    }
                },
            messages:{
                benutzername:{
                    required:'Bitte einen Benutzernamen eingeben',
                    minlength:'Bitte mindestens 2 Zeichen eingeben',
                    remote:'Dieser Benutzername ist schon vorhanden.'
                },
            }
        });
    });
[removed]
//controller
Code:
//---------------------------------
// AJAX REQUEST, IF Benutzer EXISTS
//---------------------------------
        function register_benutzer_exists () {
            $this->load->model('nutzer_model');
            if (array_key_exists('benutzername', $_POST)) {
                if ($this->benutzer_exists($this->input->post('benutzername')) == TRUE) {
                    echo json_encode(FALSE);
                } else {
                    echo json_encode(TRUE);
                }
            }
        }
//model
Code:
//---------------------------------
// Benutzer EXISTS (true or false)
//---------------------------------
        private function benutzer_exists ($benutzer) {
            $this->db->where('benutzername', $benutzer);
            $query = $this->db->get('bvs_nutzer');
            if ($query->num_rows() > 0) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
#2

[eluser]PhilTem[/eluser]
You receive a different error message than a "page not found". What you get is an internal server error

Quote:NetworkError: 500 Internal Server Error

In most cases this happens when you have some line of code that makes your sever go like "ermahgerd".
I think the problem causing line(s) are somewhere else because your posted lines look pretty good and faultless. Though there might be an error emerging from
Code:
if (array_key_exists('benutzername', $_POST)) {

Try changing it to
Code:
if ( $this->input->post('benutzername') ) {

and see if it works.
#3

[eluser]santorini[/eluser]
I've made ​​the change..
remote validation still does not work.
post data in firebug is existing...

view in firebug:
Code:
POST:
Parameterapplication
benutzername roberto
Quelle
benutzername=roberto

ANSWER:
The action you have requested is not allowed.
more tips?

thanks


#4

[eluser]InsiteFX[/eluser]
jsonp

jsonp
#5

[eluser]CroNiX[/eluser]
Sounds like you have CSRF protection enabled, but aren't sending the CSRF cookie value along with your form values in the ajax call. As far as I know, the error message "The action you have requested is not allowed." is only caused by CSRF.
#6

[eluser]santorini[/eluser]
CSRF protection was enabled, but if the CSRF protection is off
the ajax request should be executed...I understand this correctly?

Unfortunately, the validation still does not work.

I don't understand that...
#7

[eluser]LuckyFella73[/eluser]
I'm confused about your urls ...

Your base_url / site_url seems to be " http://localhost:8888/work/ci_buchverwaltung/"

If you call 'redaktion/nutzer/register_benutzer_exists' then you call a controller
named "redaktion" and the included method "nutzer"

In that case "register_benutzer_exists" would be a parameter (send to "nutzer" method)
and not a method.

Or do you route somehow?
#8

[eluser]santorini[/eluser]
actually i will call a controller named 'nutzer' and the included method 'register_benutzer_exists'. the controller 'nutzer' is inside a folder 'named 'redaktion'.

my routing:
Code:
$route['default_controller'] = "redaktion/login";
$route['redaktion'] = 'redaktion/login';
$route['redaktion/(:any)'] = 'redaktion/$1';
$route['404_override'] = '';

this should be okay?
#9

[eluser]LuckyFella73[/eluser]
I'm no routing expert so I can't comment that Wink

If I were you I would start debugging on an easier level.
First fire a simple ajax request to your validation method and return
a json debug message (log in console). If you get your response go on and add
features. Can be hard to find a bug if you have to many
functionality at once sometime ..

Best wishes - Luckyfella




Theme © iAndrew 2016 - Forum software by © MyBB