user1010101
user1010101

Reputation: 1658

Passing form values via Ajax

I would like to pass a input value via ajax.

This is the Html code.

 <form action="GET">
 <div id="username" data-role="fieldcontain">     
        <input type="text" name="username" placeholder="User Name" />
          </div>

        <div id="password" data-role="fieldcontain">   
           <input type="password" name="password" id="txtId" placeholder="Password"/>
         </div>


      <a data-role="button" id="log"  data-theme="b" href="#page2" data-transition="slide">Login</a>

  </form>

This is the Script i have.

<script>

 $.ajax({
        url: "http://1xx.1xx.0.1xx:8081/script.login",
            type: "GET",
            data: { 'page':'create_user', 'access':'user','username': +username.val(), 'password': +password.val()},
            dataType: "text",
            success: function (html) {

            }

    });


</script>

In the script and in the data: row i am using username.val() and password.val() to get the input from the div rather than hard coding the username and password.

Please pardon my web development knowledge as i am very new to this. It is not working. What have i done wrong?

Upvotes: 0

Views: 114

Answers (2)

Hasib Tarafder
Hasib Tarafder

Reputation: 5813

First of all <form action="GET"> is wrong. it must be <form method="GET">

For myself assign id to the every textfield and get the value input by jquery Selector. So,

Your Html code sholud looks like following..

 <form method="GET">
 <div id="username" data-role="fieldcontain">     
        <input type="text" id="username" name="username" placeholder="User Name" />
          </div>

        <div id="password" data-role="fieldcontain">   
           <input type="password" name="password" id="password" placeholder="Password"/>
         </div>


      <a data-role="button" id="log"  data-theme="b" href="#page2" data-transition="slide">Login</a>

  </form>

And Script should like following..

<script>

 $.ajax({
        url: "http://1xx.1xx.0.1xx:8081/script.login",
            type: "GET",
            data: { 'page':'create_user', 'access':'user','username': +$('#username').val(), 'password': +$('#password').val()},
            dataType: "text",
            success: function (html) {

            }

    });


</script>

Upvotes: 2

Orel Eraki
Orel Eraki

Reputation: 12196

Instead of username.val() use $("input[name='username']").val() and for password as well.

Upvotes: 1

Related Questions