Visualizzazione dei risultati da 1 a 4 su 4

Discussione: ajax php problema

  1. #1

    ajax php problema

    ciao a tutti sn nuovo registrato . io sto facendo delle prove con ajax perchè è da poco che lo uso ho creato un semplice script con ajax del login ma quando faccio click sul pulsante login non ma da nessun messaggio non riesco capire cosa sbaglio qualche aiuto grazie mille
    codice:
    <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>
    codice:
    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;
             }
            }
        }
    }
    Codice PHP:
    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;
            }
        } 
    shiviphpdevelopment

  2. #2
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655
    Ciao, c'e' un errore nel content type settato per POST
    Questo e' corretto
    xmlhttpRequestFunc.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    e per lo status 200 finita la richiesta va usato appunto status
    xmlhttpRequestFunc.status == 200

    http://www.w3schools.com/ajax/ajax_x...quest_send.asp

    Ad ogni modo per semplificare il tutto usando librerire tipo Jquery sarebbe tutto più semplice e con molteplici funzionalità già pronte anche crossbrowser.

    http://api.jquery.com/jQuery.ajax/

  3. #3
    ok ho modificato ma non va anche se provo solo xmlhttpRequestFunc.status == 200
    non so che problema ce si lo so io cmq uso jquery però volevo imparare qanche senza usare jquery ok lasciamo stare cavolo non riesco capire cosa sbaglio
    [CODE]
    if(xmlhttpRequestFunc.readyState == 4 && xmlhttpRequestFunc.status == 200){
    var htmlElement = document.getElementById("feedback");
    htmlElement.innerHTML = xmlhttpRequestFunc.responseText;
    }
    /CODE]
    shiviphpdevelopment

  4. #4
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655
    L ho provato in locale e funziona.
    Ho modificato con questo
    xmlhttpRequestFunc.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    perche' mancava Content-type
    e lo status a 200 con if(xmlhttpRequestFunc.status == 200){....
    Così com'e' scritto, response.php deve stare allo stesso livello del file che lo utilizza oppire dargli il path/url assoluto.

    Questo il mio
    codice:
    <script type="text/javascript">
    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","application/x-www-form-urlencoded");
        xmlhttpRequestFunc.send(logdata);
        xmlhttpRequestFunc.onreadystatechange = function(){
         if(xmlhttpRequestFunc.readyState == 4){
             if(xmlhttpRequestFunc.status == 200){
                var htmlElement = document.getElementById("feedback");
                htmlElement.innerHTML = xmlhttpRequestFunc.responseText;
             }
            }
        }
    }
    </script>
    
    <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">xxxx</div>
           </div>
    
           </div>
    PHP
    codice:
    <?php
    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;
            }
        }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.