PHP IF函数中显示的JavaScript弹出窗口,不带onClick

PHP IF函数中显示的JavaScript弹出窗口,不带onClick,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我在YouTube上观看了一个关于点击按钮后如何显示弹出框的教程。这相当简单,但现在我想稍微改变一下。我想在PHP IF函数中显示标记 我相信创建一个JavaScript函数将是一条必由之路,但我并不精通JavaScript/jQuery,因为我现在才刚刚开始。 如果我的PHP IF函数等于TRUE,我想显示以下标记 <div id="popup-box" class="popup-position"> <div class="popup-wrapper"> &

我在YouTube上观看了一个关于点击按钮后如何显示弹出框的教程。这相当简单,但现在我想稍微改变一下。我想在PHP IF函数中显示标记

我相信创建一个JavaScript函数将是一条必由之路,但我并不精通JavaScript/jQuery,因为我现在才刚刚开始。 如果我的PHP IF函数等于TRUE,我想显示以下标记

 <div id="popup-box" class="popup-position">
    <div class="popup-wrapper"> <!-- move away from screen and center popup -->
        <div class="container"> <!-- backgorund of pop up -->
            <h2>Pop box<h2>
            <p><a href="javascript:void(0)">Close popup</a></p>
        </div>
    </div>
  </div>
下面的JavaScript函数在我所关注的教程中使用。当onClick触发它时,它可以完美地工作

<script>
function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}
</script>
我有以下PHP脚本

function cart($userEmailAdd){
    global $dbc; // database connection variable
    /* 
     Verify if the product that is being added already exists in the cart_product table.
     Should it exist in the cart then display popup box with an appropriate 
     message. 

     Otherwise, add the product to cart_product
    */
    if(isset($_GET['cart'])){
    $productID = $_GET['cart'];

        $queryCheckCart = "SELECT * from cart_product WHERE emailOfCustomer = '$userEmailAdd' AND  cpProductid = '$productID'"; 

        $executeCheckCart = mysqli_query($dbc, $queryCheckCart) or die (mysqli_error($dbc));

        if(mysqli_num_rows($executeCheckCart) > 0 ){

        /* IF MYSQLI_NUM_ROWS is greater than zero then 
        it means that the product already exists in the cart_product table. 
        Then display following markup*/

            ?>
            <div id="popup-box" class="popup-position">
                <div class="popup-wrapper"> <!-- move away from screen and center popup -->
                    <div class="container"> <!-- backgorund of pop up -->
                        <h2>Pop box<h2>
                        <p><a href="javascript:void(0)">X</a></p>
                    </div>
                </div>
            </div> <!-- -->
            <?php 

        } else {

            $query = "INSERT INTO cart..." ;
            // rest of script continues after this for insertion of the product

在不使用onClick显示标记的情况下,如何使用相同或类似的函数

您可以执行类似的操作:

function cart($userEmailAdd){
global $dbc; // database connection variable
/* 
 Verify if the product that is being added already exists in the cart_product table.
 Should it exist in the cart then display popup box with an appropriate 
 message. 

 Otherwise, add the product to cart_product
*/
if(isset($_GET['cart'])){
$productID = $_GET['cart'];

    $queryCheckCart = "SELECT * from cart_product WHERE emailOfCustomer = '$userEmailAdd' AND  cpProductid = '$productID'"; 

    $executeCheckCart = mysqli_query($dbc, $queryCheckCart) or die (mysqli_error($dbc));

    if(mysqli_num_rows($executeCheckCart) > 0 ){

    /* IF MYSQLI_NUM_ROWS is greater than zero then 
    it means that the product already exists in the cart_product table. 
    Then display following markup*/

        ?>
         <script>
            $(document).ready(function(){
                toggle_visibility('popup-box');
            });
        </script>
        <div id="popup-box" class="popup-position">
            <div class="popup-wrapper"> <!-- move away from screen and center popup -->
                <div class="container"> <!-- backgorund of pop up -->
                    <h2>Pop box<h2>
                    <p><a href="javascript:void(0)">X</a></p>
                </div>
            </div>
        </div> <!-- -->
        <?php 

    } else {

        $query = "INSERT INTO cart..." ;
        // rest of script continues after this for insertion of the product
您所要做的就是告诉javascipt它必须打开弹出窗口。在这里,我让Javascript运行函数切换“弹出框”;文档加载后


popup div不必位于php的IF语句中。而不是使用$document.readyfunction{};您可以在元素中使用onLoad=toggle\u可见性“popup-box”属性。

您只需添加内联css display:block,以便在页面加载时默认显示弹出窗口

<div id="popup-box" style="display:block" class="popup-position">
然后编辑弹出窗口的关闭按钮,告诉他调用Togle_visibility onclick

<p><a href="javascript:toogle_visibility('popup-box')">X</a></p>
当然,在结束body元素之前,您需要将toggle_可见性函数更好地放在脚本标记中

<script>
function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}
</script>

您可以使用一个简单的布尔变量来指示是否显示弹出窗口,而不是在php if子句中执行html块:

$showPopup = false;

if(mysqli_num_rows($executeCheckCart) > 0 ){

    /* IF MYSQLI_NUM_ROWS is greater than zero then 
    it means that the product already exists in the cart_product table. 
    Then display following markup*/

    $showPopup = true;

 ?>

 <?php 

} else {
然后在html代码中,您可以根据$showPopup的设置显示弹出窗口:

<div id="popup-box" <?php echo ($showPopup === false)? 'style="display:none"' : '' ?> class="popup-position">

</div>

什么东西不适合您的代码?@erwan使用JavaScript函数。我想在不使用onClick的情况下显示标记。我已经设法用onClick来显示它,但是现在我想在if函数中不使用onClick来显示它,该函数测试从执行的第一个查询返回的行数。您是否尝试用ajax来执行此操作??这将很简单