cordovaphonegap-buildphonegap-desktop-app

Connecting a phone app (phonegap) to a localhost file(in local machine)


I am creating a phonegap mobile app(android).I have emulated the app using my phone.When I do a registration on the form in local machine it is successfully and data is submitted to database.But when I try to do the same using my phone nothing happens.The reason I think is the phone can't reach the loop back address of my machine.How can I go about this?

registration file (HTML)

<script type="text/javascript">
    $(document).ready(function(){  
    $("#signup").click(function(){
    var name = $("#name").val();
    var email = $("#email").val();
    var username=$("#username").val();
    var password=$("#password").val();

    // Returns successful data submission message when the entered information is stored in database.
    var dataString = 'name='+ name + '&email='+ email + '&username=' + username + '&password=' + password;

    if(name==''|| email=='' || username=='' || password=='')
    {
        alert("Please Fill All Fields");
    }
    else
    {
        //AJAX code to submit form.
        $.ajax({
                type: "POST",
                url: "http://127.0.0.1/test/signup.php",
                data: dataString,
                cache: false,
                success: function(result){
                                    alert(result);
                                        }
        });
    }
    return false;
    });
    });
</script>
    
<div class="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">Sign Up</h3>
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <input type="text" name="name" id="name" class="form-control" placeholder="Full Name" required>
                    <br>
                    <input type="email" name="email" id="email" class="form-control" placeholder="Email Address" required>
                    <br>
                    <input type="text" name="username" id="username" class="form-control" placeholder="Username" required>
                    <br>
                    <input type="password" name="password" id="password" class="form-control" placeholder="Password" required>
                    <br>
                    <input type="submit" id="signup" value="Submit" class="btn btn-success">
                </div>
            </div>
            <b>or Sign In <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#login-mod">Here</a></b>
        </div>
    </div>

Signup file (PHP file)

<?php
header('Access-Control-Allow-Origin: *');
$con=mysqli_connect("localhost","root","","test") or die("error in server");

 
  	$name=$_POST['name'];
  	$email=$_POST['email'];
  	$username=$_POST['username'];
  	$password=$_POST['password'];

  	 $sql="INSERT INTO signup(name,email,username,password) values('$name','$email','$username',sha1('$password')) ";
  	 $qry=mysqli_query($con,$sql) or die(mysqli_error($con));
     if($qry){echo "Registration was successful.";}
     else echo "A network error occured.Please retry.";
  
?>


Solution

  • If you are using local network use local network IP address. And make sure your firewall is OFF.