[英]How to redirect to a html page from a javascript snippet?
I am new to javascript. 我是javascript新手。 I want that when a user enters the username at the time of signup and that name already exists in the database,then i will show a prompt that
Name already exists
. 我希望当用户在注册时输入用户名并且该名称已经存在于数据库中时,那么我将提示您
Name already exists
。 and you need to enter another username. 并且您需要输入另一个用户名。
Now i want to have a button in the same prompt box clicking on which, the user will be redirected to the Signup page signup.html
again. 现在,我想在同一提示框中单击一个按钮,然后单击该按钮,该用户将再次重定向到“注册”页面
signup.html
。
I am unable to figure out how to do this. 我无法弄清楚该怎么做。 What i have done is this---
我所做的就是这个-
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script>
prompt("Username already exists\n please enter another username");
</script>
</body>
</html>
https://developer.mozilla.org/en-US/docs/Web/API/Window/location https://developer.mozilla.org/zh-CN/docs/Web/API/Window/location
you can put it in any js-script block you like. 您可以将其放在您喜欢的任何js脚本块中。 eg
例如
<script>
function redirectMe(url){
window.location = url;
}
setTimeout(redirectMe('http://stackoverflow.com'}, 3000);
</script>
will redirect to stackoverflow after 3000ms (=3s) 将在3000ms(= 3s)后重定向到stackoverflow
To check if a user already exists in a database, you will have to make some server-side code, not only javascript (client-side). 要检查用户是否已经存在于数据库中,您将必须编写一些服务器端代码,而不仅仅是javascript(客户端)。
You have to use AJAX to make a call to a server script (in PHP or ASP for example), sending the name entered by the user. 您必须使用AJAX来调用服务器脚本(例如,在PHP或ASP中),并发送用户输入的名称。 The server-side script will check by making a SQL query to check in the DB, and return its response to your javascript if yes or not the user already exists.
服务器端脚本将通过执行SQL查询来进行检查,以检入数据库,如果用户已经存在,则将其响应返回给javascript。
Here is an example using jQuery : 这是使用jQuery的示例:
HTML 的HTML
Your name : <input type="text" id="username" />
<button id="myButton" value="check if user exists" />
JAVASCRIPT 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>
PHP 的PHP
myscript.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;
Here is the main idea :) try to make a JSFiddle code and try in it, and write here if you have some more problems ! 这里是主要思想:)尝试制作一个JSFiddle代码并尝试使用它,如果还有其他问题,请在此处编写!
Regards, 问候,
Julien 朱利安
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.