+ Reply to Thread
Results 1 to 5 of 5

Thread: ajax problem

  1. #1
    jrobert755 is offline x10Hosting Member jrobert755 is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    15

    ajax problem

    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 Code:
    <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 Code:
    <?php
        $username
    =$_GET['username'];
        
    $passwordsha1($_GET['password']);

        if(!
    get_magic_quotes_gpc()) {
            
    $usernameaddslashes($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.";
        }
    ?>
    Codename: Prometheus: New RPG game in development. Please visit the forums at http://fourm.codenameprometheus.co.cc (this is the only option as the main site is down).

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: ajax problem

    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 by misson; 03-24-2009 at 10:58 PM.

  3. #3
    mfurqanabid's Avatar
    mfurqanabid is offline x10Hosting Member mfurqanabid is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    37

    Re: ajax problem

    Hi

    stop using old techniques. Use jQuery object and i am sure it will give you more benefit.

    jQuery site: http://www.jquery.com/
    jQuery ajax reference: http://docs.jquery.com/Ajax

  4. #4
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: ajax problem

    Definitely change the button in your form.

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

    <input type="button" onclick="ajaxFunction()" /> is better.
    Last edited by VPmase; 03-27-2009 at 11:11 AM.

  5. #5
    jrobert755 is offline x10Hosting Member jrobert755 is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    15

    Re: ajax problem

    thanks guys for the help. and especially for changing from submit to a button. my script now works. and i will look into jQuery when i have more time. thanks again.

+ Reply to Thread

Similar Threads

  1. AJAX Refresh problem with IE
    By oracle in forum Programming Help
    Replies: 9
    Last Post: 06-05-2008, 02:09 PM
  2. DB number problem
    By lionheart8 in forum Free Hosting
    Replies: 5
    Last Post: 04-08-2008, 08:26 AM
  3. Site Problem
    By himgar in forum Free Hosting
    Replies: 3
    Last Post: 03-10-2008, 03:36 PM
  4. Ad code problem!
    By Akkarin in forum Free Hosting
    Replies: 8
    Last Post: 08-29-2005, 10:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers