简体   繁体   中英

How to phonegap send a parameter ajax to php file

I have a problem about my project. I make it in two part: On host server by php and On mobile and I want to include all in one project. How should I do that?

I want to send data to php by ajax and I did the following :

I have this javascript :

    <script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
    $("#button").click(function(evt){
    var txtUsername = $("#txtUsername").val();
    var txtPassword1 = $("#txtPassword1").val();

    var senddata ={'txtUsername' : txtUsername,'txtPassword1':txtPassword1};
    $.ajax({
        type: 'POST',
        data: senddata,
        url: 'http://(Domain_name)/a_project/file/check_login.php',
        cache: false,
        success: function(data) {

            alert('Your comment was successfully added');
        },
        error: function(){

            alert('There was an error adding your comment');

        }
    });

    return false;
});
}
</script>

HTML code :

   <form method="post" data-ajax="false">

 <label for="user">ชื่อผู้ใช้ : </label>
 <input type="text" data-clear-btn="true" name="txtUsername" id="txtUsername"   data-theme="a">
 <label for="pass">รหัสผ่าน :</label>
 <input type="password" data-clear-btn="true" name="txtPassword1" id="txtPassword1"  data-theme="a">
   <table align="center" width="100%">
  <tr >
  <td width="50%"><input type="button" id="button" value="เข้าสู่ระบบ" data-theme="g" ></td>
  <td width="50%"><a href="#popupForgot" data-rel="popup" data-position-to="window" data-role="button"  data-theme="g">ลืมรหัสผ่าน</a></td> </tr>
  </table>

  <a href="#popupRegis" data-rel="popup" data-position-to="window" data-role="button" data-theme="g">สมัครสมาชิก</a>

php code :

<?
$txtUsername =$_POST['txtUsername'];
$txtPassword1 =$_POST['txtPassword1'];
include("connect.php");
$strSQL = "SELECT * FROM login WHERE username = '".$txtUsername."' 
and password = '".$txtPassword1."'";

 .
 .
 .
 .
 .

 ?>

Well, it looks like the data can't send to php. what should i do?

THANK YOU

This can't work if the app is running from the phone because of Access-Control-Allow-Headers being thrown by your server as your POST request is from another host.
You would need to use JSONP AJAX Call for this which is basically a GET request

Have a look at JSON crossdomain communication with PHP file and a local javascript file and http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/

Don't forget to add "callback=?" and to change your $_POST variables to $_GET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM