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 10 of 10

Thread: ajax php

  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ajax php

    i have made simple ajax script to do some test with login but when i ckick on login button it gives me no result no message
    Code:
    <div id="wrapper">
            <div id="form">
                <form action="response.php" method="post">
                <label>Username</label>
                <input type="text" name="logusername" id="logusername">
                <lable>Password</lable>
                <input type="password" name="logpassword" id="logpassword">
                <input type="submit" name="logsubmit" id="logsubmit" value="LOGIN" onclick="getConnectionResponse();return false;">
                </form>
                <div id="feedback"></div>
           </div>
           
           </div>
    Code:
    function getXML_HTTP(){
        var xmlHTTP = null;
        if(window.XMLHttpRequest){
            xmlHTTP = new XMLHttpRequest();
        }else if(window.ActiveXObject){
            xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlHTTP;
    }
    
    function getConnectionResponse(){
        var xmlhttpRequestFunc = getXML_HTTP();
        var pageURL = "response.php";
        var logusername = document.getElementById("logusername").value;
        var logpassword = document.getElementById("logpassword").value;
        var logdata = 'logusername='+logusername+'&logpassword='+logpassword;
        xmlhttpRequestFunc.open("POST", pageURL ,true);
        xmlhttpRequestFunc.setRequestHeader("Content-type","x-www-form-urlencoded");
        xmlhttpRequestFunc.send(logdata);
        xmlhttpRequestFunc.onreadystatechange = function(){
         if(xmlhttpRequestFunc.readyState == 4){
             if(xmlhttpRequestFunc.readyState == 200){
                var htmlElement = document.getElementById("feedback");
                htmlElement.innerHTML = xmlhttpRequestFunc.responseText;
             }
            }
        }
    }
    Code:
    if(isset($_POST['logusername']) && isset($_POST['logpassword'])){
            $logusername = $_POST['logusername'];
            $logpassword = $_POST['logpassword'];
            if(empty($logusername) || empty($logpassword)){
                echo "enter username and password";
            }else{
                echo "your name is ".$logusername." and your password is ".$logpassword;
            }
        }

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,509
    Thanks
    22
    Thanked 567 Times in 560 Posts
    Code:
         if(xmlhttpRequestFunc.readyState == 4){
             if(xmlhttpRequestFunc.readyState == 200){
    this will never enter the condition (200 ≠ 4). you confused .status with .readyState.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    now i am doing in this way but no result it always send to me on response.php page
    Code:
    function getConnectionResponse(){
        var xmlhttpRequestFunc = getXML_HTTP();
        var pageURL = "response.php";
        var logusername = document.getElementById("logusername").value;
        var logpassword = document.getElementById("logpassword").value;
        var logdata = 'logusername='+logusername+'&logpassword='+logpassword;
        xmlhttpRequestFunc.open("POST", pageURL ,true);
        xmlhttpRequestFunc.setRequestHeader("Content-type","x-www-form-urlencoded");
        xmlhttpRequestFunc.send(logdata);
        xmlhttpRequestFunc.onreadystatechange = function(){
         if(xmlhttpRequestFunc.readyState == 4){
                var htmlElement = document.getElementById("feedback");
                htmlElement.innerHTML = xmlhttpRequestFunc.responseText;
            }
        }
    }

  4. #4
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,509
    Thanks
    22
    Thanked 567 Times in 560 Posts
    it always send to me on response.php page
    then make the submit button into a click button.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  5. #5
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not work if know something about ajax ok otherwise please don't give any stupid answer thanks very much

  6. #6
    Senior Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    1,064
    Thanks
    196
    Thanked 131 Times in 131 Posts
    not work if know something about ajax ok otherwise please don't give any stupid answer thanks very much
    And you should learn to show some respect thanks very much.

    LT
    0000

  7. #7
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,471
    Thanks
    31
    Thanked 1,008 Times in 1,005 Posts
    @shivi_php in Dormilich's first post he meant
    if(xmlhttpRequestFunc.readyState == 4){
    if(xmlhttpRequestFunc.readyState == 200){
    is wrong and that it should be
    if (xmlhttpRequestFunc.readyState == 4) {
    if (xmlhttpRequestFunc.status == 200) {
    In his next post he says this
    <input type="submit" name="logsubmit" id="logsubmit" value="LOGIN" onclick="getConnectionResponse();return false;">
    is wrong because a type="submit" will always SUBMIT and onclick="getConnectionResponse(); will never work unless you
    Quote Originally Posted by Dormilich View Post
    then make the submit button into a click button.
    It has nothing to do with AJAX.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  8. #8
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok i try with <button onClick></button> but not work also i am using xmlhttpRequestFunc.status == 200 but no result here ok leave it thanks very much

  9. #9
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,509
    Thanks
    22
    Thanked 567 Times in 560 Posts
    a button without explicit type is a submit button.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  10. #10
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,471
    Thanks
    31
    Thanked 1,008 Times in 1,005 Posts
    One last error in the ajax request
    xmlhttpRequestFunc.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    Add that
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


 

Posting Permissions

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