如何将RESTful Web服务与php和ajax集成

时间:2012-07-18 09:40:55

标签: php ajax rest

我有一个像这样的个人资料创建页面。

<html>
    <head>
        <script>
            function validateMe(){

               var xmlhttp;
               var userEmail = document.getElementById("email").value;
               var userPwd = document.getElementById("pwd").value;
               //alert(userEmail);
                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)
                    {
                        //alert(xmlhttp.responseText);
                    }
                    document.getElementById("responds").innerHTML=xmlhttp.responseText;
                    }

                xmlhttp.open("POST","api.php?email="+userEmail+"&pwd="+userPwd,true);
                xmlhttp.send();  
            }
        </script>
    </head>
    <body>
        <form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table>
                <tr><td colspan="2">Login</td></tr>
                <tr><td>Username:</td><td><input type="text" name="email" id="email"/></td></tr>
                <tr><td>Password:</td><td><input type="text" name="pwd" id="pwd"/></td></tr>
                <tr><td><input type="button" value="Submit" name="submit" onclick="validateMe();"/><td></td></tr>
            </table>
            <div id="responds"></div>
        </form>
    </body>
</html>

<html> <head> <script> function validateMe(){ var xmlhttp; var userEmail = document.getElementById("email").value; var userPwd = document.getElementById("pwd").value; //alert(userEmail); 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) { //alert(xmlhttp.responseText); } document.getElementById("responds").innerHTML=xmlhttp.responseText; } xmlhttp.open("POST","api.php?email="+userEmail+"&pwd="+userPwd,true); xmlhttp.send(); } </script> </head> <body> <form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr><td colspan="2">Login</td></tr> <tr><td>Username:</td><td><input type="text" name="email" id="email"/></td></tr> <tr><td>Password:</td><td><input type="text" name="pwd" id="pwd"/></td></tr> <tr><td><input type="button" value="Submit" name="submit" onclick="validateMe();"/><td></td></tr> </table> <div id="responds"></div> </form> </body> </html>

我需要使用带有php和ajax的RESTful Web服务将这些数据插入我的MySql数据库。

我该如何实现呢?任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

首先,我建议你使用jQuery Javascript框架。有了它,它可以简化AJAX请求并获取输入值(不用担心Web浏览器等)。

其次,我在这里看不到任何RESTful Web服务。这是一个简单的POST表单。

所以请详细说明你的问题:)

相关问题