简体   繁体   English

“提交”表格并直接指向Javascript生成的URL

[英]“Submit” Form and Direct to Javascript Generated URL

I have a login form that, when completed, sends users to a page with a JavaScript generated URL (allowing me to pass a JavaScript variable to my PHP script using $_GET). 我有一个登录表单,完成后会将用户发送到带有JavaScript生成的URL的页面(允许我使用$ _GET将JavaScript变量传递给我的PHP脚本)。 However, in order to do that, the Login button is currently 'type="button"'. 但是,为此,登录按钮当前为'type =“ button”'。 While everything works, it means that users cannot login by hitting Enter; 一切正常后,这意味着用户无法通过按Enter键登录; they must actually click the Login button. 他们实际上必须单击“登录”按钮。 Is there a way I can "Submit" the form, while still having it point to the JavaScript generated URL? 有没有一种方法可以“提交”表单,同时仍使其指向JavaScript生成的URL?

This seems like a pretty basic concept, which tells me I might be approaching it the wrong way to begin with. 这似乎是一个非常基本的概念,它告诉我可能是从错误的方法入手。 Any guidance is appreciated. 任何指导表示赞赏。

HTML: HTML:

<form name="login">
    Username: <input type="text" name="user_id"/>
    Password: <input type="password" name="pswrd"/>
    <input type="button" onclick="check(this.form)" value="Login"/>
</form>

JavaScript: JavaScript的:

function check(form) {
    var userCredentials = [["jsmith", "smithpassword", "John Smith"], ["jdoe", "doepassword", "Jane Doe"]];
    var credCheck = 0;
    for (var i = 0; i < userCredentials.length; i++) {
        if (userCredentials[i][0] == form.user_id.value) {
            credCheck += 1;
            var displayName = userCredentials[i][2];
            if (userCredentials[i][1] == form.pswrd.value) {
                window.open("home.php?display_name=" + displayName, "_self");
            } else {
                alert('The username and password do not match.');
                return false;
            }
        }
    }
    if (credCheck == 0) {
        alert('The username entered is not valid.');
        return false;
    } else {
        return true;
    }
}

Instead of opening php page via javascript, you need to change the form action dynamically to point to your generated url. 而不是通过javascript打开php页面,您需要动态更改表单action以指向您生成的url。

HTML: HTML:

<form name="login">
    Username: <input type="text" name="user_id"/>
    Password: <input type="password" name="pswrd"/>
    <input type="submit" onclick="check(this.form)" value="Login"/>
</form>

JavaScript: (line 9 & 10 changed) JavaScript :(第9行和第10行已更改)

function check(form) {
    var userCredentials = [["jsmith", "smithpassword", "John Smith"], ["jdoe", "doepassword", "Jane Doe"]];
    var credCheck = 0;
    for (var i = 0; i < userCredentials.length; i++) {
        if (userCredentials[i][0] == form.user_id.value) {
            credCheck += 1;
            var displayName = userCredentials[i][2];
            if (userCredentials[i][1] == form.pswrd.value) {
                form.action = "home.php?display_name=" + displayName;
                return true;
            } else {
                alert('The username and password do not match.');
                return false;
            }
        }
    }
    if (credCheck == 0) {
        alert('The username entered is not valid.');
        return false;
    } else {
        return true;
    }
}

You could try: 您可以尝试:

<form name="login" onsubmit="check(this)">
    Username: <input type="text" name="user_id"/>
    Password: <input type="password" name="pswrd"/>
    <input type="submit" value="Login"/>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM