1

Why I am not getting values from inputs? I checked it with alert box and there's no values. I do not see any wrong codes or maybe i might have forgotten something to write on my code

<div class="login" align="left">
                        <input type="input" class="user" placeholder="Username"/>
                        <input type="password" class="pass" placeholder="Password"/>
                        <button name="login" class="btnLogin" id="btnLogin">LOGIN</button>
                        <div class="register"><a href="register.php" class="reg">Register Now!</a></div>
                    </div>



    <script>
$(document).ready(function(){


    $(document).on('click','#btnLogin',function(){

        var username = $('.user').text();
        var password = $('.pass').text();
        alert(username); //------------>this alert box is null
        $.ajax ({
            url:"login_func.php",  
            method:"POST",  
            data:{username:username, password:password},  
            dataType:"text", 
            success:function(data){
                alert(data); 
            }
        });
    });

});
        </script>
    </body>
1
  • 1
    instead of text ...use val() Commented Dec 3, 2016 at 6:11

6 Answers 6

1

Use val() and not text()

$('.user').val();

For all kind of input elements , select and textarea you use val() to get its value. And rest elements you use text() to get the inner text of those elements

Sign up to request clarification or add additional context in comments.

Comments

1

Value from input fields are normally accessed through .val.

$(document).ready(function(){
    $(document).on('click','#btnLogin',function(){

        var username = $('.user').val();
        var password = $('.pass').val();
        alert(username); //------------>this alert box is null
        $.ajax ({
            url:"login_func.php",  
            method:"POST",  
            data:{username:username, password:password},  
            dataType:"text", 
            success:function(data){
                alert(data); 
            }
        });
    });

});

Comments

1

You need to use .val() instead of .text() since you are trying to get a valuye of the input field. .text() should be used for a text element like <div>, <p> etc.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body><div class="login" align="left">
                        <input type="input" class="user" placeholder="Username"/>
                        <input type="password" class="pass" placeholder="Password"/>
                        <button name="login" class="btnLogin" id="btnLogin">LOGIN</button>
                        <div class="register"><a href="register.php" class="reg">Register Now!</a></div>
                    </div>



    <script>
$(document).ready(function(){


    $(document).on('click','#btnLogin',function(){

        var username = $('.user').val();
        var password = $('.pass').val();
        alert(username); //------------>this alert box is null
        $.ajax ({
            url:"login_func.php",  
            method:"POST",  
            data:{username:username, password:password},  
            dataType:"text", 
            success:function(data){
                alert(data); 
            }
        });
    });

});
        </script>
    </body>

Comments

1

Input does not have to be retrieved by text but by val().

check this snippet.

$(document).ready(function() {


  $(document).on('click', '#btnLogin', function() {

    var username = $('.user').val();
    var password = $('.pass').val();
   
    alert(username); //------------>this alert box is null
    $.ajax({
      url: "login_func.php",
      method: "POST",
      data: {
        username: username,
        password: password
      },
      dataType: "text",
      success: function(data) {
        alert(data);
      }
    });
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="login" align="left">
  <input type="input" class="user" placeholder="Username" />
  <input type="password" class="pass" placeholder="Password" />
  <button name="login" class="btnLogin" id="btnLogin">LOGIN</button>
  <div class="register"><a href="register.php" class="reg">Register Now!</a>
  </div>
</div>



<script>
</script>
</body>

Hope it helps

Comments

1

use .val() instead of .text() to retrieve values. and change the input type of user from input to text.i.e. <input type="text" class="user" placeholder="Username"/>

Comments

0

Change your input type to text for username and use val() instead text()

$(document).ready(function(){


    $(document).on('click','#btnLogin',function(){

        var username = $('.user').val(); //here you need to use val()
        var password = $('.pass').val(); //here you need to use val()
        alert(username+ ", "+ password); //------------>this alert box is null
        $.ajax ({
            url:"login_func.php",  
            method:"POST",  
            data:{username:username, password:password},  
            dataType:"text", 
            success:function(data){
                alert(data); 
            }
        });
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="login" align="left">
    <input type="text" class="user" placeholder="Username"/> <!--here you need to add type text-->
    <input type="password" class="pass" placeholder="Password"/>
    <button name="login" class="btnLogin" id="btnLogin">LOGIN</button>
    <div class="register"><a href="register.php" class="reg">Register Now!</a></div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.