0

now, I'm making a chatbox. This is the code of the page were you can type in your name, but I will, when a user type my name (name of the webmaster) that the user got an error, because there aren't the webmaster. My if-else function won't work. Is there anyone who can help me? Thanks

<html>
    <head>
<title></title>
<style type="text/css">

    #main{
            width: 400px;
            height: 300px;
            background-color: #333;
            text-align: center;
            position: absolute;
            color: white;
            left:50%;
            margin-left: -200px;
            top: 50%;
            margin-top: -150px;
            }

        </style>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">

        function show() {
        var text = document.getElementById('text');
        if (text.value == "Jonathan Cazaerck"){alert("Deze gebruiker mag u niet gebruiken");}
        else{
        $.post("processing.php", { stage: "newuser", username: $("#text").val()}, function(data){
        window.location = ("newsession.php?user="+data);}
        );}
  </script>
    </head>
    <body>
    <div id="main"><br><br><br><br>
    Geef je voor- en achternaam op: <br><br><input type="text" size="35" id="text"/><button id="send" onclick="show();">Verzenden</button>
    </div>
    </body>
</html>
3
  • 1
    You use document.getElementById, but then use jQuery. Why not just use var text = jQuery('#text').val(); then pass text in the post to processing.php? Commented Jul 2, 2012 at 13:22
  • You should also use test /Jonathan Cazaerck/.test(text.value) or trim your value that you get from the input box. If you don't and the user adds an extra space at the end of your name the comparison will fail. Commented Jul 2, 2012 at 13:29
  • As a side note, make sure the server is doing the same check as well. Otherwise a malicious user could call the action with your name manually. The server should never trust data coming from the client. Commented Jul 2, 2012 at 13:30

2 Answers 2

1

You need one more } at the end of your code.

    <script type="text/javascript">

    function show() {
    var text = document.getElementById('text');
    if (text.value == "Jonathan Cazaerck"){alert("Deze gebruiker mag u niet gebruiken");}
    else{
    $.post("processing.php", { stage: "newuser", username: $("#text").val()}, function(data){
    window.location = ("newsession.php?user="+data);}
    );}
    }

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm...according to SF, we answered at the exact same second.
No worries :) Be sure to examine your browser's JavaScript console when you write code. It can easily tell you when you have simple little problems like this.
1

You're missing a closing } in your show function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.