Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Simple username input check problem - AJAX

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 400 Views
  • Johnny9
    Community Member
    19 Posts

    Simple username input check problem - AJAX Link to this post

    Hi, I am trying to make validation of a username field. Checks if exist and if exist, shows "choose another name" via ajax.

    But problem is, that no value is passed to variable via Ajax. $_POST['username'] is empty.

    So my code in controller:

    public function checkuser1(){

          $Params = $this->getURLParams();      
    if($Params['ID'] == '1'){
                if (Director::is_ajax()) {
                      $username = $_POST['username']; // Seems that I dont receive any value from input vie ajax

                      $username = mysql_real_escape_string($username);
                      $check_for_username = mysql_query("SELECT ID FROM Member WHERE UserName='$username'");
                         return '1';
                         return '0';
                else {
                   return Director::redirect(Director::baseURL());


    public function checkuser(){

          return $this->checkuser1();


    And JS code:

    <script type="text/javascript">

    $(".container .inner .top .right").html("<img src='ajax-loader.gif' /> checking...");

                   var username = $("#Form_RegistrationForm_UserName").val();
                   var dataString = 'username='+ username;

                               if(server_response == '0'){
                                  $(".container .inner .top .right").html("Username available");
                                  $(".container .inner .top .right").html("Username already taken");



    If anyone knows what is the problem, let me know Thanks for any help!

  • Willr
    Forum Moderator
    5489 Posts

    Re: Simple username input check problem - AJAX Link to this post

    Try using the web inspector in your web browser, investigate the XHR panel to see what the request is being made (and ensure it contains your post data). I think the jQuery post argument takes an object rather than a string (i.e { 'username': username } rather than username=)

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.