Search code examples
javascriptphpajaxcordovaphonegap

Display php echo from php file on a html cordova page


So basically, I'm building a phonegap app, and there's a table that has some information that I have to show on the app (html). I managed to get the login system working, but now I cant figure out how to display an echo from php to my app.

PHP Code:

<?php
$con = mysqli_connect("localhost","root","", "database") or die("connection error");
$id = $_POST['id'];

if(isset($_POST['button']))
{   
    $select = mysqli_num_rows(mysqli_query($con, "SELECT * FROM `table` WHERE `id`='$id'"));
    echo $select;
}
mysqli_close($con);
?>

So, basically, the process is the user inputs an ID and clicks a button, and then I get the data from the table row with that ID and display it on the app. Maybe it's with AJAX but I just can't find a way to make it work. Thanks in advance.

EDIT:

Still not able to output the echo in html. Here's the javascript:

function yourFunction(id) {
        if (navigator.connection.type == Connection.NONE) {
    alert('An internet connection is required to continue');
        } else {
    alert(navigator.connection.type);
    $.ajax({
        url: 'http://localhost/StudyBuddy/StudyBuddy/www/yourScript.php',
        data: {
              id: id
        }   
        success: function (data) {
            alert(data);
            $("#data").html(data);
        },      
        error: function(data,r,t){
            alert(t); 
        }
    })
}
}

And the html code:

<input id="id" type="text" required placeholder='&#61447;  ID'/></span></td>
<input class="button" type="submit" id="button"  value="ID"></td>
    <p id="data"></p>

Solution

  • You cannot use PHP with Cordova.

    However, you can use ajax to do what you want.

    You could do something like :

    function yourFunction(id) {
        if (navigator.connection.type == Connection.NONE) {
            alert('An internet connection is required to continue');
        } else {
            alert(navigator.connection.type);
            $.ajax({
                url: 'https://yourScript.php',
                data: {
                      id: id
                }   
                success: function (data) {
                    alert(data);
                },      
                error: function(data,r,t){
                    alert(t); 
                }
            })
        }
    }
    

    And your php would look something like :

    <?php
        $con = mysqli_connect("localhost","root","", "database") or die("connection error");
    $id = $_POST['id'];
    
    if(isset($_POST['button']))
    {   
        $select = mysqli_num_rows(mysqli_query($con, "SELECT * FROM `table` WHERE `id`='$id'"));
        echo $select;
    }
    mysqli_close($con);
    ?>
    

    Be careful about your <meta> Because you could get an error like this if you are not in https:

    Security error: Failed to execute 'open' on 'XMLHttpRequest': refused to connect to 'website...'because it violates the document's content security policy