如何从javascript片段重定向到html页面?

如何从javascript片段重定向到html页面?,javascript,html,Javascript,Html,我是javascript新手。我希望当用户在注册时输入用户名,并且该名称已经存在于数据库中时,我将显示一个提示,提示名称已经存在。您需要输入另一个用户名 现在我希望在同一个提示框中有一个按钮,单击该按钮,用户将再次重定向到注册页面Signup.html 我不知道该怎么做。 我所做的就是这样--- 在此处插入标题 提示(“用户名已存在\n请输入其他用户名”); 你可以把它放在任何你喜欢的js脚本块中。乙二醇 <script> function redirectMe(url){

我是javascript新手。我希望当用户在注册时输入用户名,并且该名称已经存在于数据库中时,我将显示一个提示,提示
名称已经存在
。您需要输入另一个用户名

现在我希望在同一个提示框中有一个按钮,单击该按钮,用户将再次重定向到注册页面
Signup.html

我不知道该怎么做。 我所做的就是这样---


在此处插入标题
提示(“用户名已存在\n请输入其他用户名”);

你可以把它放在任何你喜欢的js脚本块中。乙二醇

<script>
  function redirectMe(url){
    window.location = url;
  }

  setTimeout(redirectMe('http://stackoverflow.com'}, 3000);
</script>

函数重定向我(url){
window.location=url;
}
设置超时(重定向到我)http://stackoverflow.com'}, 3000);
3000ms(=3s)后将重定向到stackoverflow

你可以把它放在任何你喜欢的js脚本块中

<script>
  function redirectMe(url){
    window.location = url;
  }

  setTimeout(redirectMe('http://stackoverflow.com'}, 3000);
</script>

函数重定向我(url){
window.location=url;
}
设置超时(重定向到我)http://stackoverflow.com'}, 3000);
3000ms(=3s)后将重定向到stackoverflow

window.location.href="signup.html"
参考资料

使用

window.location.href="signup.html"
参考资料


要检查数据库中是否已经存在用户,您必须编写一些服务器端代码,而不仅仅是javascript(客户端)

您必须使用AJAX调用服务器脚本(例如在PHP或ASP中),发送用户输入的名称。服务器端脚本将通过进行SQL查询来检查数据库,如果用户是否已经存在,则将其响应返回到javascript

下面是一个使用jQuery的示例:

HTML

Your name : <input type="text" id="username" />
<button id="myButton" value="check if user exists" />
您的姓名:
JAVASCRIPT

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script>
    $(function () {

        //when you will click on your button
        $('#myButton').click(function(){
            //get prompted username
            var username = $('#username').val();
            //check if username exists making AJAX call
            checkUserName(username);
        });

    });

    function checkUserName(name)
    {
        //ajax call to your server-side script
        $.ajax({
            url: 'myscript.php',
            type: 'POST',
            data: 'username='+name,
            success: function (response) {
                //if response is 1, a user with this name already exists
                if(response == 1)
                {
                    alert('user already exists');
                }
                //the name is available
                else
                {
                    //redirect to your "signup.html" page
                    window.location.href="signup.html"
                    alert('name is available');

                }
            }
        });
    }
</script>

$(函数(){
//当你点击你的按钮时
$(“#myButton”)。单击(函数(){
//获取提示用户名
var username=$('#username').val();
//检查进行AJAX调用时是否存在用户名
检查用户名(用户名);
});
});
函数检查用户名(名称)
{
//对服务器端脚本的ajax调用
$.ajax({
url:'myscript.php',
键入:“POST”,
数据:“用户名=”+名称,
成功:功能(响应){
//如果响应为1,则具有此名称的用户已存在
如果(响应==1)
{
警报(“用户已存在”);
}
//名称可用
其他的
{
//重定向到“signup.html”页面
window.location.href=“signup.html”
警报(“名称可用”);
}
}
});
}
PHP

myscript.php

<?php

//you have to secure and check the variable, depending what you will use, mysqli_real_escape_string, or escape properly the received variables
$username = $_POST['username'];

//query example, you have to ajust depending on your database & user table
$sql = 'SELECT COUNT(username) FROM db.user WHERE username = "'.$username.'"';

//exec your query

//if the count returned result is > 0, there is a user with this name, return 1 for example
//else, user with this name does not exist, return 0 for example

if($return == 1)
    echo "1";
else
    echo "0";

exit;

要检查数据库中是否已经存在用户,您必须编写一些服务器端代码,而不仅仅是javascript(客户端)

您必须使用AJAX调用服务器脚本(例如在PHP或ASP中),发送用户输入的名称。服务器端脚本将通过进行SQL查询来检查数据库,如果用户是否已经存在,则将其响应返回到javascript

下面是一个使用jQuery的示例:

HTML

Your name : <input type="text" id="username" />
<button id="myButton" value="check if user exists" />
您的姓名:
JAVASCRIPT

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script>
    $(function () {

        //when you will click on your button
        $('#myButton').click(function(){
            //get prompted username
            var username = $('#username').val();
            //check if username exists making AJAX call
            checkUserName(username);
        });

    });

    function checkUserName(name)
    {
        //ajax call to your server-side script
        $.ajax({
            url: 'myscript.php',
            type: 'POST',
            data: 'username='+name,
            success: function (response) {
                //if response is 1, a user with this name already exists
                if(response == 1)
                {
                    alert('user already exists');
                }
                //the name is available
                else
                {
                    //redirect to your "signup.html" page
                    window.location.href="signup.html"
                    alert('name is available');

                }
            }
        });
    }
</script>

$(函数(){
//当你点击你的按钮时
$(“#myButton”)。单击(函数(){
//获取提示用户名
var username=$('#username').val();
//检查进行AJAX调用时是否存在用户名
检查用户名(用户名);
});
});
函数检查用户名(名称)
{
//对服务器端脚本的ajax调用
$.ajax({
url:'myscript.php',
键入:“POST”,
数据:“用户名=”+名称,
成功:功能(响应){
//如果响应为1,则具有此名称的用户已存在
如果(响应==1)
{
警报(“用户已存在”);
}
//名称可用
其他的
{
//重定向到“signup.html”页面
window.location.href=“signup.html”
警报(“名称可用”);
}
}
});
}
PHP

myscript.php

<?php

//you have to secure and check the variable, depending what you will use, mysqli_real_escape_string, or escape properly the received variables
$username = $_POST['username'];

//query example, you have to ajust depending on your database & user table
$sql = 'SELECT COUNT(username) FROM db.user WHERE username = "'.$username.'"';

//exec your query

//if the count returned result is > 0, there is a user with this name, return 1 for example
//else, user with this name does not exist, return 0 for example

if($return == 1)
    echo "1";
else
    echo "0";

exit;

我需要在何处插入此代码?以及如何调用此函数?我在下面写了代码
提示符(“用户名已存在\n请输入另一个用户名”);window.location=“SignUp.html”
,它现在重定向。
提示符()
显示一个空行,但如果我不需要空行该怎么办。我只想显示一条消息,然后单击按钮,它会重定向到
signup.html
。怎么做?谢谢@ManuKaracho,我使用了
alert()
执行我想要的操作。我需要在何处插入此代码?以及如何调用此函数?我在下面编写了代码
提示符(“用户名已存在\n请输入另一个用户名”);window.location=“SignUp.html”
并立即重定向。
提示符()
显示一个空行,但如果我不需要空行该怎么办。我只想显示一条消息,然后单击按钮,它会重定向到
signup.html
。怎么做?谢谢@ManuKaracho,我使用
alert()
来做我想做的事。可能重复的可能重复