1 Replies - 5676 Views - Last Post: 12 January 2013 - 12:51 AM Rate Topic: -----

#1 cyberphoenix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-January 13

Ajax php login query via onlick

Posted 12 January 2013 - 12:10 AM

Hello,

I am building a webpage and i found that most of my functions requires the whole webpage to reload. for example login the page will reload and display success or error. I found out that ajax can be used. the problem is that i don't know how.sI made this code and it does not work. I did it by onchange but not using onlick. this is my code fragment
<title>Untitled Document</title>
</head>
	<script>
        function call_login(username, password)
        {
           
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET","user_login.php?u="+username+"&p="+password,true);
        xmlhttp.send();
        }
        
        
            var klient;
            function ajax(filurl) {
                 klient = new XMLHttpRequest();
                 klient.onreadystatechange = hanterare;
                 klient.open("GET", filurl);
                 klient.send(null);
                }
            function hanterare() {
                document.getElementById("utdata").innerHTML= klient.responseText;
            }
            
        </script>

<body>
<form>
    <p>USERNAME: 
    <input type="text" name="usermame" id="username">
    </p>
    <p>PASSWORD:
      <label for="password"></label>
      <input type="password" name="password" id="password">
    </p>
    <p>
      <input type="submit" name="call_login" id="call_login" value="Submit" onclick="call_login(document.getElementById('username'), document.getElementById('password')">
    </p>
</form>
<label for="call_login"></label>
</body>
</html>

Hope somebody helps. Thanks!

This post has been edited by Dormilich: 12 January 2013 - 12:42 AM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Ajax php login query via onlick

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: Ajax php login query via onlick

Posted 12 January 2013 - 12:51 AM

there are several problems:
- the onclick handler is missing a )
- the username and password parameters are passed form elements, not strings (i.e. they are converted to the string [object HTMLInputElement], which will definitely not log you in)
- ajax() will not work in older IEs
- the "utdata" element does not exist
- the button click will load a new page. then why bother using AJAX?
- the <form>s required action attribute is missing
- the upper part oft the HTML header (DTD, <head>, <title>) is missing (might be a copy error)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1