javascript - How to call a PHP from a mobile app? -
i have html page takes login user , authenticates , redirects different page. converted web-page app using phonegap app build function. trying call php thats on server. proper way so? below code.
html
<script> function postdata() { // 1. create xhr instance - start var xhr; if (window.xmlhttprequest) { xhr = new xmlhttprequest(); } else if (window.activexobject) { xhr = new activexobject("msxml2.xmlhttp"); } else { throw new error("ajax not supported browser"); } // 1. create xhr instance - end // 2. define when xhr feed response server - start xhr.onreadystatechange = function () { if (xhr.readystate === 4) { if (xhr.status == 200 && xhr.status < 300) { document.getelementbyid('div1').innerhtml = xhr.responsetext; } } } // 2. define when xhr feed response server - start var userid = document.getelementbyid("userid").value; var pid = document.getelementbyid("pid").value; // 3. specify action, location , send server - start xhr.open('post', 'www.xyz.com/abc/login.php'); xhr.setrequestheader("content-type", "application/x-www-form-urlencoded"); xhr.send("userid=" + userid + "&pid=" + pid); // 3. specify action, location , send server - end } </script> </head> <body> <form> <label for="userid">user id :</label><br/> <input type="text" name ="userid" id="userid" /><br/> <label for="pid">password :</label><br/> <input type="password" name="password" id="pid" /><br><br/> <div id="div1"> <input type="button" value ="login" onclick="postdata()" /> </div> </form>
try jquery.
function postdata() { $.ajax({ url: "/ajax/login_check.php", type: "post", data: {userid : $("#userid").val(),pid : $("#pid").val()}, cache: false, success: function (result) { if(result.statu){ //valide div screen show } else{ //invalide div screen show } } }); }
check valide login use check file
login_check.php
ur mysql , php code put in file.
if { //if valide use $responce['statu'] = "1"; $responce['msg'] = "login success."; } else{ $responce['statu'] = "0"; $responce['msg'] = "invalide use name & pass"; } echo json_encode ($responce); exit;
Comments
Post a Comment