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
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,135
    Thanks
    12
    Thanked 332 Times in 328 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
    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
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,135
    Thanks
    12
    Thanked 332 Times in 328 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
    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
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    838
    Thanks
    172
    Thanked 90 Times in 90 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
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 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.

  • #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
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,135
    Thanks
    12
    Thanked 332 Times in 328 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
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 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.


  •  

    Posting Permissions

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