【问题标题】:How do you type php variables in js code?你如何在js代码中输入php变量?
【发布时间】:2020-05-02 05:11:59
【问题描述】:

所以,如果用户存在于名为 paymentindue 的表中,我想向用户显示 js 警报。该表有一个自定义消息列。我希望警报显示自定义消息。我正在使用 PHP 来处理 sql 查询

    <?php 
include 'dbconn.php';
$name = $_SESSION['name'];
$email = $_SESSION['email'];

$sql = "SELECT * FROM users where email='$email' AND username = '$name'";
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0){
            echo '<script>alert("<?php Custom message goes here?>")</script>';
        }
?>

但问题是我收到的是警报而不是自定义消息

【问题讨论】:

标签: javascript php sql


【解决方案1】:

您需要将 echo 拆分为两个字符串和一个 php 变量才能使其正常工作。

$your_custom_message = 'Your message here'
echo '<script>alert("' . $your_custom_message . '")</script>'`

【讨论】:

  • 那行不通。您正在向用户发送原始 php。
【解决方案2】:

您似乎正在尝试向用户发送 php 代码,但这是行不通的。相反,如果需要,只需在脚本中编写消息即可。

$message = "something you compose here";
echo '<script>alert("'. $message .'")</script>';

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 2014-06-03
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多