ajax problem

jrobert755

New Member
Messages
15
Reaction score
0
Points
0
here is my problem. i am creating a login script with html and ajax that will check if the information is correct. but, it is targeting itself instead of the intended webpage. how can i fix it. here is the code:

login.php
HTML:
<html>
<script language="javascript" type="text/javascript">
<!-- 
        //Browser Support Code
        function ajaxFunction(){
            var ajaxRequest;  // The variable that makes Ajax possible!
    
            try{
                // Opera 8.0+, Firefox, Safari
                ajaxRequest = new XMLHttpRequest();
            } catch (e){
                // Internet Explorer Browsers
                try{
                    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e){
                        // Something went wrong
                        alert("Your browser broke!");
                        return false;
                    }
                }
            }
            // Create a function that will receive data sent from the server
            ajaxRequest.onreadystatechange = function(){
                if(ajaxRequest.readyState == 4){
                    var ajaxDisplay = document.getElementById('display');
                    ajaxDisplay.innerHTML = ajaxRequest.responseText;
                }
            }
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;
            var queryString = "?username=" + username;
            ajaxRequest.open("GET", "tester.php" + queryString, true);
            ajaxRequest.send(null); 
        }
        //-->
</script>

<form>
Username: <input name="username" type="text" /><br />
Password: <input name="password" type="password" /><br />
<input type="submit" onclick='ajaxFunction()' />
<div id="display"></div>
</form>
</html>
tester.php
PHP:
<?php
    $username=$_GET['username'];
    $password= sha1($_GET['password']);

    if(!get_magic_quotes_gpc()) {
        $username= addslashes($username);
    }
    
    include("config.php");
    
    $result=mysql_query("SELECT * FROM username WHERE username='$username'") or die(mysql_error());

    if(mysql_num_rows($result) == "1")
    {
        $test=mysql_fetch_array($result);
        $testa =$test['password'];
        $actkey= $test['activated'];

        if($actkey == "1")
        {
            if($password == $testa) {
                echo "Success!";
            } else {
                echo "Incorrect information. Please try again";
            }
        }
        else
        {
            echo "Your account has not been activated.";
        }
    }
    else
    {
        echo "You entered an incorrect username. Please try again.";
    }
?>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The page is redirecting because, by default, browsers redirect when a form is submitted. To prevent this, try at least one of:
  1. use a push button rather than a submit button
  2. add 'onsubmit="return false"' attribute to form element.
  3. add an event listener (via addEventListener/attachEvent) that calls preventDefault() on the event.
Personally, I recommend both 1 and 2. 1 because a push button is more suited to your use and 2 to prevent form submission if the user presses the enter key.

Edit:
I hope you're not using GET to send the login info in your production code. It's much safer to POST it as url-encoded data (ie a Content-type of "application/x-www-form-urlencoded") via HTTPS.
 
Last edited:

VPmase

New Member
Messages
914
Reaction score
0
Points
0
Definitely change the button in your form.

<input type="submit" onclick='ajaxFunction()' /> is dysfunctional.

<input type="button" onclick="ajaxFunction()" /> is better.
 
Last edited:

jrobert755

New Member
Messages
15
Reaction score
0
Points
0
thanks guys for the help. and especially for changing from submit to a button. my script now works. :biggrin: and i will look into jQuery when i have more time. thanks again.
 
Top