繁体   English   中英

在html cordova页面上显示php文件中的php echo

[英]Display php echo from php file on a html cordova page

因此,基本上,我正在构建一个phonegap应用程序,并且有一个表包含一些必须在该应用程序(html)上显示的信息。 我设法使登录系统正常工作,但是现在我不知道如何显示从php到我的应用程序的回显。

PHP代码:

<?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);
?>

因此,基本上,该过程是用户输入一个ID并单击一个按钮,然后从具有该ID的表行中获取数据并将其显示在应用程序上。 也许它与AJAX一起使用,但我只是找不到使它起作用的方法。 提前致谢。

编辑:

仍然无法以html输出回声。 这是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); 
        }
    })
}
}

和html代码:

<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>

您不能将PHP与Cordova一起使用。

但是,您可以使用ajax来执行所需的操作。

您可以执行以下操作:

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); 
            }
        })
    }
}

和你的PHP看起来像:

<?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);
?>

请注意您的<meta>因为如果您不在https中,则可能会收到如下错误:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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