简体   繁体   English

如何使用Javascript来“存储”和“调用”数据?

[英]How do i use Javascript to “store” and “call” data?

I'm making a "Catalog" page for a fake shop site, when i click one of the thumbnails shown on the page a java-script overlay is meant to show up with all the information for the product (such as a larger image 'photo1') with that thumbnail (The products are in an SQL database). 我正在为假商店网站制作“目录”页面,当我单击页面上显示的缩略图之一时,Java脚本叠加层旨在显示产品的所有信息(例如较大的图像“ photo1')和该缩略图(产品在SQL数据库中)。

While this does pull up an overlay as intended it doesn't get just the one related 'photo1', it instead gets all of them from the table. 尽管这样做确实可以按预期拉上叠加层,但它不仅得到了一个相关的“ photo1”,而是从表格中获取了所有叠加层。

I've had help from a teacher but she couldn't even help, but from what i gather i need a way to store what thumbnail was chosen so i can clarify what info to pull for the overlay. 我从老师那里得到了帮助,但她什至没有帮助,但是从我的收集中,我需要一种方法来存储选择的缩略图,以便我可以弄清楚要叠加的信息。

Main: 主要:

<div id="overlay">
    <div>
    <?php
        include 'includes/connection.php';

        try {
            $sql = 'SELECT * FROM item;';
            $resultSet = $pdo->query($sql);
        } // end try
        catch (PDOException $e) {
            echo 'Error fetching items: ' . $e->getMessage();
            exit();
        } // end catch

        foreach ($resultSet as $row) {
            // put each rows's elements into variables
            $itemName = $row['itemName'];
            $unitPrice = $row['unitPrice'];
            $qtyOnHand = $row['qtyOnHand'];
            $thumbNail = $row['thumbNail'];
            $photo1 = $row['photo1'];

            echo '<td><img  src="' .$photo1 .'" width="500" height="500" alt="$itemName" title="$itemName" ></td>';
        }
    ?>

    <p>Content you want the user to see goes here.</p>
Click here to [<a href='#' onclick='overlay()'>close</a>]
    </div>
</div>

<div id="MainHolder">
    <div id="Headerbar">
        <?php
            include 'includes/login.php';
        ?>
        <div class="ContentBody">
            <div class="Content">
                <article> 
                    <h2>Store</h2>
                    <?php
                        include 'includes/connection.php';
                        try
                        {
                            $sql = 'SELECT * FROM item;';
                            $resultSet = $pdo->query($sql);
                        } // end try
                        catch (PDOException $e)
                        {
                            echo 'Error fetching items: ' . $e->getMessage();
                            exit();
                        } // end catch
                    ?>
                    <!-- open table -->

                    <article>
                    <?php
                        // read result set and populate table 
                        foreach ($resultSet as $row)
                        {
                            // put each rows's elements into variables
                            $itemName = $row['itemName'];
                            $thumbNail = $row['thumbNail'];
                            $qtyOnHand = $row['qtyOnHand'];
                            // test for out of stock condition
                            // if no stock, display out of stock image else display add to cart image 
                            if ($qtyOnHand <= 0) {
                                echo '<td><img  src="images/outOfStock.png" border="3" class="floatingImage" height="80" width="80" alt="" title=""></td>';
                            } else {
                                echo '<td><img class="floatingImage" border="3" src="' .$thumbNail .'" width="80" height="80" alt="' .$itemName .'" title="' .$itemName .'" onclick="overlay()" ></td>';
                            }
                        } // end foreach
                        // close table 
                        echo '</article>';
                    ?>
                </article>
             </div>
             <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
        </div>          
    </div>
</div>

Javascript: 使用Javascript:

function overlay() {
    el = document.getElementById("overlay");
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}

The overlay and catalog are in the same file. 叠加层和目录位于同一文件中。 I'm still learning, so my apologies for any messy formatting/code. 我仍在学习,因此对任何混乱的格式/代码表示歉意。

EDIT: I've merged some of the code, this shows pretty much my whole store page other than Headers, ect... 编辑:我已经合并了一些代码,这几乎显示了我的整个商店页面,而不是标题,等等...

You need to edit your overlay function to send the itemName to server, this will tell your server which item the user clicked on. 您需要编辑覆盖函数,以将itemName发送到服务器,这将告诉您的服务器用户单击了哪个项目。

overlay function: 叠加功能:

function overlay() {

    var item = this.getAttribute("title");//this gets the name of item that was clicked
}

Now we need to set up an ajax request to the server. 现在我们需要向服务器设置一个ajax请求。

function ajaxRequest(item) {
  if (window.XMLHttpRequest){
    var xmlhttp= new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
      if (xmlhttp.readyState==4 && xmlhttp.status == 200){//show the overlay after we recieve a response from the server
        el = document.getElementById("overlay");
        el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
        el.innerHtml = ''; //remove previous response
        el.innerHTML=xmlhttp.responseText; //insert the response in your overlay
      }
    }
    xmlhttp.open("GET","overlay.php?item="+ encodeURIComponent(item),true);
    xmlhttp.send();
  }
}

Now we need to edit the overlay function to make a call to the ajaxRequest function: 现在,我们需要编辑overlay函数来调用ajaxRequest函数:

function overlay() {

    var item = this.getAttribute("title");//this gets the name of item that was clicked
    ajaxRequest(item); //send the item name to server
}

After doing this, your PHP will receive a the variable on your server. 完成此操作后,您的PHP将在您的服务器上收到一个变量。 Create a new file called overlay.php and insert the following code. 创建一个名为overlay.php的新文件,并插入以下代码。 Save this file in the same directory as your Store.php file. 将此文件保存在与Store.php文件相同的目录中。

overlay.php: overlay.php:

<?php
if (isset($_GET['item'])) {//check if you received the name
    $itemName = $_GET['item'];

   //query database for the itemName

                        try
                        {
                            $sql = 'SELECT * FROM item WHERE itemName ='.$itemName.';';//just select data with the matching item name.
                            $resultSet = $pdo->query($sql);
                        } // end try


                        catch (PDOException $e)
                        {
                            echo 'Error fetching items: ' . $e->getMessage();
                            exit();
                        } // end catch
    //fetch the data
    foreach ($resultSet as $row) {
        // put each rows's elements into variables
        $itemName = $row['itemName'];
        $unitPrice = $row['unitPrice'];
        $qtyOnHand = $row['qtyOnHand'];
        $thumbNail = $row['thumbNail'];
        $photo1 = $row['photo1'];
    echo '<td><img  src="' .$photo1 .'" width="500" height="500" alt="$itemName" title="$itemName" ></td>';
    }//end foreach

}//end if
?>

如果要绝对在客户端存储数据,则可以将html5的本地存储功能与嵌入式sql lite数据库一起使用,以使用Javascript存储和检索信息。

When you first get the Product from the DB using this loop: 首次使用此循环从数据库获取产品时:

foreach ($resultSet as $row) {
    // put each rows's elements into variables
    $itemName = $row['itemName'];
    $unitPrice = $row['unitPrice'];
    $qtyOnHand = $row['qtyOnHand'];
    $thumbNail = $row['thumbNail'];
    $photo1 = $row['photo1'];
    echo '<td><img  src="' .$photo1 .'" width="500" height="500" alt="$itemName" title="$itemName" ></td>';
}

Are those all the attributes that you want to show when the User clicks on the image for the overlay? 当用户单击叠加层的图像时,是否要显示所有这些属性? If so, just store the values in the actual <img> tag: 如果是这样,只需将值存储在实际的<img>标记中:

    echo '<td><img src="' .$photo1 .'" width="500" height="500" alt="$itemName" title="$itemName" data-unit-price="$unitPrice" data-qty="$qtyOnhand"></td>';

Then you can use Javascript to access the data and show in your overlay. 然后,您可以使用Javascript访问数据并在叠加层中显示。

I try to understand your problem and used jQuery to solve it. 我尝试了解您的问题,并使用jQuery解决了它。

First you have to load jQuery lib 首先,您必须加载jQuery lib

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

And add display:none style to every production info in overlay(default is hidden). 并在叠加中的每个生产信息中添加display:none样式(默认为隐藏)。

echo '<td><img src="' .$photo1 .'" width="500" height="500" alt="$itemName" title="$itemName" style="display:none" ></td>';

Remove thumbnail inline onClick event trigger 删除缩略图内联onClick事件触发器

echo '<td><img class="floatingImage" border="3" src="' .$thumbNail .'" width="80" height="80" alt="' .$itemName .'" title="' .$itemName.'"'></td>';

Add this click event handler thus jQuery can show the production info which user clicked. 添加此单击事件处理程序,以便jQuery可以显示用户单击的生产信息。

$(".floatingImage").click(function(){
    var itemName = $(this).attr('alt');
    $('#overlay img').hide();
    $('#overlay img[alt="'+itemName+'"]').show();
    overlay();
});

Hope this is helpful for you. 希望这对您有帮助。

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

相关问题 如何使用AJAX提取数据并将其存储在javascript变量中? - How can I use AJAX to fetch data and store it in javascript variables? 如何使用其他目录存储会话数据 - How do I use other directory to store session data 如何使用普通 JavaScript 访问 AJAX 调用中返回的数据? - How do I access the data returned in an AJAX call with vanilla JavaScript? 如何将更改的数据存储到数据库中? - How do I store changing data into a database? 如何使用 store 方法,以便它可以将数据保存到我的表中 - How do I use the store method so that it can save data to my table 如何使用externalInterface允许Flash调用javascript更新屏幕上的值? - How do I use externalInterface to allow Flash to call javascript to update a value on the screen? 如何使用 Drupal 在另一个页面中调用我的 CSS 或 JavaScript 文件并将它们包含在多个页面中 - How do I use Drupal to call my CSS or JavaScript files in another page and include them in multiple pages 我是否使用ci_sessions存储会话数据 - do i use ci_sessions to store session data 如何将SQL调用的结果存储在php数组中 - How do I store the results of an SQL call in a php array 如何存储和调用50万张图像? - How do I store and call 500k images?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM