简体   繁体   中英

How to redirect to a html page from a javascript snippet?

I am new to 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 . 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.

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

you can put it in any js-script block you like. eg

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

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

will redirect to stackoverflow after 3000ms (=3s)

Use

window.location.href="signup.html"

References

JavaScript Window Location

Window open() Method

To check if a user already exists in a database, you will have to make some server-side code, not only javascript (client-side).

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. 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.

Here is an example using 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>

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 !

Regards,
Julien

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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