1

How can i pass values from textbox to a url on click of a button using javascript? I have a login page. when i input a username and password in a textbox, the url should take the values and give the response. How can i do it?

http://url.php?id=login&email=<email>&password=<password>

This url gives response as {"status":0,"msg":"Email is Wrong!"} and when i put my id and pwd in url

http://url.php?id=login&[email protected]&password=abc

url give response as

{"status":1,"msg":"Session is active","session_id":"p246igeaadcdui7hb0o2677c53","user_id":"13"}

Thanks in advance..

function A() { $.getJSON('http:url.php?id=login&email=&password=', function (data) { alert(data.status); alert(data.msg); }); } function B() { $.getJSON('url.php?id=login&email=&password=', function (data) { alert(data.user_id); document.getElementById("login").innerHTML; }); }

Username :

Password :

This is what i am working on

5
  • 9
    You might want to reconsider putting the user's password in the URL in plaintext like that... Commented Feb 18, 2012 at 10:06
  • no i want the value to pass from text to the url as i click on a button Commented Feb 18, 2012 at 10:08
  • 1
    Yes, which means your user's email will be visible in the URL in plain text. That's a Bad Thing. Commented Feb 18, 2012 at 10:10
  • no actually right now i am working on local server. so its only for my coding. Commented Feb 18, 2012 at 10:11
  • @SONALKASLIWAL Don't ever do that. Eventually, by accident, you might just decide to use this solution in production code. Commented Feb 18, 2012 at 10:22

2 Answers 2

2

Plain javascript:

var email = document.getElementById("email").value;
var password = document.getElementById("password ").value;
var url = "http://url.php?id=login&email="+encodeURIComponent(email)+"&password="+encodeURIComponent(password);
window.location.href = url;

jQuery:

var email = $("#email").val();
var password = $("#password").val();
var url = "http://url.php?id=login&email="+encodeURIComponent(email)+"&password="+encodeURIComponent(password);
window.location.href = url;

Note that for my example to work, the email and the password input fields must have the id set to email and password respectively.

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

6 Comments

This one does not pass the var url to address bar?
what do you mean by "must have the id set to email and password respectively."
@jurka It doesn't pass it to the address bar, right. Just add window.location.href = url; (edited the question)
@SONALKASLIWAL I mean <input type="text" id="email" /> <input type="password" id="password />
function A() { $.getJSON('http:url.php?id=login&email=&password=', function (data) { alert(data.status); alert(data.msg); }); } function B() { $.getJSON('url.php?id=login&email=&password=', function (data) { alert(data.user_id); document.getElementById("login").innerHTML; }); }
|
0

ajax version:

//same as satoshi version
var myUrl = "http://url.php?id=login&email="+encodeURIComponent(email)+"& password="+encodeURIComponent(password);

$.ajax({
    url : myUrl,
    dataType : 'json',
    success : function(data){
        alert( data.status ? ("logged in " + data.msg) : ("error: " + data.msg));
    },
    error : function(jqXHR, textStatus, errorThrown){
        alert('error ' + errorThrown);
    }
});

1 Comment

function A() { $.getJSON('http:url.php?id=login&email=&password=', function (data) { alert(data.status); alert(data.msg); }); } function B() { $.getJSON('url.php?id=login&email=&password=', function (data) { alert(data.user_id); document.getElementById("login").innerHTML; }); }

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.