Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple AJAX, help needed

    I'm abit new to ajax, one of my friends helped me with an ajax script for an old site, he is now on holiday so i cant ask him. I wanted to give editing it a shot to try and login, so i had to add another value and edit the parameters, now it doesnt seem to work.

    The javascript is:
    Code:
    //Request
    var http_request = getHTTPObject();
    var url = "post_login.php?";
    
    function getHTTPObject() {
      var xmlhttp = false;
    if (window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e){
    try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){}
    }
    }
    
      return xmlhttp;
    }
    
    
    // Ajax 
       function makePOSTRequest(id) {
    
          it = document.getElementById("username").value +
    	  document.getElementById("password").value;
          parameters = 'username=' + encodeURIComponent(it) + '&password=' + encodeURIComponent(it) + '&id=' + encodeURIComponent(id);
          http_request.open('POST', url, true);
          http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          http_request.onreadystatechange = alertContents;
          http_request.send(parameters);
       }
    
       function alertContents() {
    	  if (http_request.readyState == 1) {
                document.getElementById('result').innerHTML = '<img src="loading.gif" />';             
          }
          if (http_request.readyState == 4) {
                results = http_request.responseText;
                document.getElementById('result').innerHTML = results;               
          }
       };
    The html im using is:
    PHP Code:
    echo("
     Username:<br /> <input type=\"text\" name=\"username\" class=\"nav\" /><br />
    Password:<br />
    <input type=\"password\" name=\"password\" class=\"nav\" /><br />
    <input type=\"button\" class=\"nav\" onclick=\"javascript: makePOSTRequest('"
    .$profile[id]."')\" value=\"Login!\"><br />
    <strong><a href=\"/index.php?y=register\">Not Registered?</a></strong>"
    ); 
    And the php page the ajax is using (post_login.php) is:
    PHP Code:
    ob_start();
    session_start();
    include (
    "config.php");
    $id intval($_POST[id]);

    $username $_POST['username'];
    $password md5($_POST['password']);

    $info mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    $data mysql_fetch_array($info);
    if(
    $data[password] != $password)
    {
    echo 
    "Incorrect username or password!";
    }
    else
    {
    setcookie("essex_id"$logged[id],time()+(62*60*23*10), "/""");
    setcookie("essex_pass"$logged[password],time()+(62*60*23*10), "/""");
    $_SESSION[essex_id] = $logged[id];
    $_SESSION[essex_pass] = $logged[password];

    Please can someone help me,

    Thanks

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Download Firefox and install Firebug will make your life easier.

    What is the error you are getting? Is it making it to the server?

    One thing I see:
    You need to check for a status of 200 when you get a readystate of 4.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how do i do that?

    When i click login nothing happens, no error just no attempt to submit the data

  • #4
    New Coder
    Join Date
    Jul 2006
    Location
    Switzerland
    Posts
    57
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This is just an answer to my problems - if you get a functioning results please let me know. I wonder why I didnt see this thread before. Im a begginer in Ajax just like you are but I guess you're a bit further than I am but I intend to spend the next available 10 days practising.

    Talking about rumbling.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •