在javascript中将值从文本框传递到url

时间:2012-02-18 10:04:37

标签: javascript html

如何使用javascript点击按钮时将值从文本框传递到网址?我有一个登录页面。当我在文本框中输入用户名和密码时,网址应该取值并给出响应。我该怎么做?

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

此网址的回复为{"status":0,"msg":"Email is Wrong!"},当我将我的ID和密码放入网址时

http://url.php?id=login&email=abc@gmail.com&password=abc

url给出回复

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

提前致谢..

    function A(){         $ .getJSON('http:url.php?id = login&amp; email =&amp; password =',function(data){             警报(data.status);             警报(data.msg);         });     }     function B(){         $ .getJSON('url.php?id = login&amp; email =&amp; password =',function(data){             警报(data.user_id);             的document.getElementById( “登录”)的innerHTML。         });     }

用户名:

密码:
 

这就是我正在做的事情

2 个答案:

答案 0 :(得分:1)

普通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;

请注意,要使我的示例正常工作,电子邮件和密码输入字段的ID必须分别设置为电子邮件和密码。

答案 1 :(得分:0)

ajax版本:

//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);
    }
});