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 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation cant pass values to Ajax function..

    I am using Ajax with PHP to authenticate id and pass at my site.
    while the php script is working fine, the ajax is giving problems..
    The http://127.0.0.1/abhishek.html? comes to http://127.0.0.1/abhishek.html?usern...llo&pass=world where hello is the id and world is the pass instead of loading the php script "idpass.php". I guess the values from the textbox are not being passed to Ajax function..

    Code:
     function showUser(str,str1)
    {
    if (str=='')
      {
      document.getElementById("demo").innerHTML="";
      return;
      } 
    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("demo").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","idpass.php?q="+str +"&r="+str1,true);
    xmlhttp.send();
    }
    
    <form onSubmit=showUser(this.value,this.value) >
      <div id="txt"><b>Enter your username </b>
        <input type="text" name= "username">
        <br>
      <b>Enter your password</b> <input type="password" name="pass">
      <br></div>
      <div id="txt1" style="display : none">help</div>
      <input type="submit" value="Login" />
    </form>
    Last edited by cyborg911; 04-11-2011 at 04:12 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Ok, I think you should consider a few things here

    1) Put all attribute values into double quotes
    2) on-handlers should be written in lower case
    3) A form does not have a value ... so this.value in onsubmit is pointless
    4) You should return false from your onsubmit handler, otherwise the page will be submitted and refreshed afterwards

    Something like that:
    Code:
    <form onsubmit="showUser(); return false;">
    In showUser() you can access the fields much easier if you give them id attributes like
    Code:
    <input type="text" name= "username" id="user">
    Then you can get the value using
    Code:
    document.getElementById('user').value

  • Users who have thanked devnull69 for this post:

    cyborg911 (04-11-2011)


  •  

    Posting Permissions

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