简体   繁体   English

在Jscript弹出窗口中显示PHP结果

[英]Show PHP result in Jscript Pop-up

i want a user to be able to check a number in the database by putting in some details. 我希望用户能够通过输入一些细节来检查数据库中的数字。 This number would then be shown in a javascript alert. 然后,此数字将显示在javascript警报中。

I have the index page with the user input as seen below: 我有一个带有用户输入的索引页面,如下所示:

<script type="text/javascript">
    function check()
    {
        $.ajax({
            url: "checkpna.php",
            type: "POST"})
    }

    function show_alert()
    {
        alert(check())
    }


</script>


<form action="#" method="get" class="formubuntu" onSubmit="return submitForm()">
    <p>Check Pallet PNA Number &nbsp;<input name="lpn" id="lpn_0" type="text" style="border-top-left-radius: 4px 4px; border-top-right-radius: 4px 4px; border-bottom-right-radius: 4px 4px; border-bottom-left-radius: 4px 4px; margin-right: auto; margin-left: auto;" value="Pallet LPN">
    &nbsp;<input name="submit" type="submit" value="Submit" class="art-button" onclick="show_alert()"></p>
</form>

and the checkpna.php which checks the specific details in the DB: 以及检查数据库中特定细节的checkpna.php:

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if($_GET['submit'] == "Submit")
{
    $lpn = $_GET["lpn"];

}

$values = array();

$query = "select serial_number from t_container where container_number = '".$lpn."';";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo stripslashes($row['serial_number']);
    }
}

else {
    echo "NO RESULTS";
}

?>

however i am getting a popup that says undefined. 但是我得到一个未定义的弹出窗口。 what am i doing wrong?? 我究竟做错了什么??

The function check does not return anything (hence undefined ). 函数check不返回任何内容(因此undefined )。

If you do return $.ajax() it does return something, but what? 如果你确实return $.ajax()返回一些东西,但是什么? It returns a Promise . 它返回一个Promise A promise is a container for a future value, a value we don't have yet. 承诺是未来价值的容器,是我们尚未拥有的价值。 The reason we don't have the value yet is because the request hasn't completed yet, it only just started. 我们没有价值的原因是因为请求尚未完成,它才刚刚开始。

You can change your code to: 您可以将代码更改为:

function check() {
    return $.ajax(/* .. */);
}
function show_alert() {
    check().done(function (result) {
        alert(result);
    });
}

There are some serious problems with your PHP code. 您的PHP代码存在一些严重问题。 It works , but it's not secure. 有效 ,但不安全。 Read up on SQL Injection and look at validating your input (you check submit , but not lpn ). 阅读SQL注入并查看验证输入(检查submit ,但不检查lpn )。

You want something like this: 你想要这样的东西:

    $.ajax({
        url: "checkpna.php",
        type: "POST"})
     .done(function( data ) {
        alert(data);
     }

You cannot alert immediately, you have to wait until you receive the answer to your Ajax request from the server. 您无法立即发出警报,您必须等到从服务器收到Ajax请求的答案。 Just add a callback function to do this, when the result arrives. 只需添加回调函数即可在结果到达时执行此操作。

https://api.jquery.com/jQuery.ajax/ https://api.jquery.com/jQuery.ajax/

function check()
    {
        $.ajax({
            url: "checkpna.php",
            type: "POST",
            complete: function(return) {
                alert(return);
            }
        })
    }

    function show_alert()
    {
        check();
    }

I believe for ajax request you need a callback when success and alert from that callback 我相信对于ajax请求,当成功并从该回调发出警报时,您需要回调

From jQuery doc 来自jQuery doc

success Type: Function( PlainObject data, String textStatus, jqXHR jqXHR ) A function to be called if the request succeeds. success类型:Function(PlainObject data,String textStatus,jqXHR jqXHR)请求成功时要调用的函数。 The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; 该函数传递三个参数:从服务器返回的数据,根据dataType参数格式化; a string describing the status; 描述状态的字符串; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. 和jqXHR(在jQuery 1.4.x,XMLHttpRequest)对象。 As of jQuery 1.5, the success setting can accept an array of functions. 从jQuery 1.5开始,成功设置可以接受一系列函数。 Each function will be called in turn. 每个函数将依次调用。 This is an Ajax Event. 这是一个Ajax事件。

<script type="text/javascript">
    function show_alert()
    {
        $.ajax({
            url: "checkpna.php",
            type: "POST",
            success: function (data) {
                alert(data);
            }
        });
    }
</script>

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

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