post - AJAX username check don't pass value -


i trying make validation of username field in silverstripe 3. ajax checks if username exist , if exist, shows "this name taken" via ajax.

but problem is, no value passed variable via ajax. $_post['username'] empty.

so code in controller:

    public function checkuser1(){        $params = $this->geturlparams();        if($params['id'] == '1'){              if (director::is_ajax()) {                    $username = $_post['username']; // seems dont receive value input vie ajax                    $username = mysql_real_escape_string($username);                   $check_for_username = mysql_query("select id member username='$username'");                   if(mysql_num_rows($check_for_username))                   {                      return '1';                   }                   else                   {                      return '0';                   }              }             else {                return director::redirect(director::baseurl());             }                        } }   public function checkuser(){        return $this->checkuser1();  } 

and js code:

<script type="text/javascript">  $(document).ready(function(){  $("#form_registrationform_username").change(function(){ $(".container .inner .top .right").html("<img src='ajax-loader.gif' /> checking...");                 var username = $("#form_registrationform_username").val();                var datastring = 'username='+ username;                    $.ajax({                         type:"post",                         url:"http://localhost/mysite/member/checkuser/1/",                         data:datastring,                         success:function(server_response){                            if(server_response == '0'){                               $(".container .inner .top .right").html("username available");                            }                            else{                               $(".container .inner .top .right").html("username taken");                            }                         }                       });                 });  });  </script> 

change

data:datastring

to

data: { username: username }

and make sure verify username var holds username value before passing it.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -