-2

我有两个文件。index.php 和 cart.php。我的所有功能、与数据库的连接等都位于文件 cart.php 中。如果用户单击 HREF 链接,我想调用一些函数。

我的 index.php:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    <div id="wrapper" align="center" style="width:90%; height:auto; margin-left:auto; margin-right:auto;">      
    <?php cart(); ?>
    <br><br>
        <div id="sidebar" align="left" style="width:15%; height:auto; background-color:#999999; float:left;">
            <ul style="list-style-type:none;">
                <li><a href="#" id="all">ALL</a></li>
                <li><a href="#" id="shirts">SHIRTS</a></li>
                <li><a href="#" id="hoodies">HOODIES</a></li>
        </ul>
        </div>
        <div id="products" style="width:85%; height:auto; background-color:#888888; float:left;">
            <?php
            products_all();
            ?>
        </div>
    </div>
</body>
</html>

那么,如何让 HREF 链接调用特定函数(位于 cart.php 中)并将其显示在 div 标签内(在我的情况下,在 div id="products" 中)?

这可能很容易,我只是一个初学者。

这是我的 cart.php 以防万一。

<?php

session_start();


$page = 'index.php';

// ***
/*
$mysql_host = "***";
$mysql_database = "***";
$mysql_user = "***";
$mysql_password = "***";
*/

// localhost
$mysql_host = "localhost";
$mysql_database = "cartt";
$mysql_user = "root";
$mysql_password = "";

mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
mysql_select_db($mysql_database) or die(mysql_error());

if (isset($_GET['add'])) {
    $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
    while ($quantity_row = mysql_fetch_assoc($quantity)) {
        if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
            $_SESSION['cart_'.(int)$_GET['add']]+='1';

        }
    }
    header('Location: '.$page); 
}

if (isset($_GET['remove'])) {
    $_SESSION['cart_'.(int)$_GET['remove']]--;
    header('Location: '.$page); 
}

if (isset($_GET['delete'])) {
    $_SESSION['cart_'.(int)$_GET['delete']]='0';
    header('Location: '.$page); 
}

function products_all() {
    $get_all = mysql_query('SELECT id, name, description, price FROM products ORDER BY id DESC');

        if (mysql_num_rows($get_all)==0) {
            echo "There are no products to display!";
        }
        else {                                      
            echo '<table width="900px" allign="center" cellpadding ="5" style="background-color:transparent;">
                  <tr align="center" valign="middle">';

                  $i = 0;

            while ($get_row = mysql_fetch_assoc($get_all)) {

                echo '<td><img src="./cartimages/'.$get_row['id'].'.jpeg" alt=" " height="225px" align="center"><br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />&pound'.number_format($get_row['price'], 2).'<br /><a href="cart.php?add='.$get_row['id'].'">Add</a></td>';                

                $i++;
                if ($i == 4) {
                    echo '</tr>
                          <tr align="center" valign="middle">';
                          $i = 0;
                }


            }
        }
    }

function products_shirts() {

    $get_shirts = mysql_query('SELECT id, name, description, price FROM products WHERE type = "shirt" ORDER BY id DESC');

        if (mysql_num_rows($get_shirts)==0) {
            echo "There are no products to display!";
        }
        else {                                      
            echo '<table width="900px" allign="center" cellpadding ="5" style="background-color:transparent;">
                  <tr align="center" valign="middle">';

                  $i = 0;

            while ($get_row = mysql_fetch_assoc($get_shirts)) {

                echo '<td><img src="./cartimages/'.$get_row['id'].'.jpeg" alt=" " height="225px" align="center"><br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />&pound'.number_format($get_row['price'], 2).'<br /><a href="cart.php?add='.$get_row['id'].'">Add</a></td>';                

                $i++;
                if ($i == 4) {
                    echo '</tr>
                          <tr align="center" valign="middle">';
                          $i = 0;
                }


            }
        }
    }

function products_hoodies() {
$get_hoodies = mysql_query('SELECT id, name, description, price FROM products where type = "hoodie" ORDER BY id DESC');

        if (mysql_num_rows($get_hoodies)==0) {
            echo "There are no products to display!";
        }
        else {                                      
            echo '<table width="900px" allign="center" cellpadding ="5" style="background-color:transparent;">
                  <tr align="center" valign="middle">';

                  $i = 0;

            while ($get_row = mysql_fetch_assoc($get_hoodies)) {

                echo '<td><img src="./cartimages/'.$get_row['id'].'.jpeg" alt=" " height="225px" align="center"><br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />&pound'.number_format($get_row['price'], 2).'<br /><a href="cart.php?add='.$get_row['id'].'">Add</a></td>';                

                $i++;
                if ($i == 4) {
                    echo '</tr>
                          <tr align="center" valign="middle">';
                          $i = 0;
                }


            }
        }
    }


function paypal_items() {
    $num = 0;
    foreach($_SESSION as $name => $value) {
        if ($value!=0) {
            if (substr($name, 0 , 5)=='cart_') {
                $id = substr($name, 5, strlen($name)-5);
                $get = mysql_query('SELECT id, name, price, shipping FROM products WHERE id='.mysql_real_escape_string((int)$id));
                while ($get_row = mysql_fetch_assoc($get)) {
                    $num++;
                    echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">';
                    echo '<input type="hidden" name="item_name_'.$num.'" value="'.$get_row['name'].'">';
                    echo '<input type="hidden" name="amount_'.$num.'" value="'.$get_row['price'].'">';
                    echo '<input type="hidden" name="shipping_'.$num.'" value="'.$get_row['shipping'].'">';
                //  echo '<input type="hidden" name="shipping2_'.$num.'" value="'.$get_row['shipping'].'">';
                    echo '<input type="hidden" name="quantity_'.$num.'" value="'.$value.'">';
                }
            }   
        }
    }
}

function cart() {
    $total = 0;
    foreach($_SESSION as $name => $value) {
        if ($value>0) {
            if (substr($name, 0, 5)=='cart_') {
                $id = substr($name, 5, (strlen($name)-5));
                $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
                while ($get_row = mysql_fetch_array($get)) {
                    $sub = $get_row['price']*$value;
                    echo $get_row['name'].' x '.$value.' @ &pound;'.number_format($get_row['price'], 2).' = &pound;'.number_format($sub, 2).'<a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br />';
                }
            }
            $total += $sub;
        }
    }
    if ($total==0) {
        echo "Your cart is empty.";
    }
    else {
        echo '<p>Total: &pound;'.number_format($total, 2).'</p>';

        ?>
        <p>
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="*************">
        <?php paypal_items(); ?>        
        <input type="hidden" name="currency_code" value="GBP">
        <input type="hidden" name="amount" value="<?php echo $total; ?>">
        <input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
        </form>
        </p>
        <?php


    }
}

?>
4

3 回答 3

1

如果您希望在客户端上执行某些操作时运行 php 函数,则必须发出调用该函数的 AJAX 请求,然后在 AJAX 请求返回时动态插入 HTML。

请记住,PHP 仅生成通过网络发送的文本作为 HTML、CSS、JavaScript、JSON、XML ...... PHP 无法直接与您的页面交互,它只能生成新内容

这是您可以使用的基本 AJAX 示例;我将使用 jQuery 和一些简化的 PHP 代码

ajax.php

function doit() { echo "anything"; }
doit(); 

page.html

<a href="#" id="mylink"> Load stuff </a>
<div id="products"></div>

page.js

$("#mylink").click(function() { 
    $('#products').load("/ajax.php");
})

doit当您单击 ID 为“page.html”的链接时,该代码会将 ajax.php 中的结果加载到 ID 为“products”的 div 中

http://api.jquery.com/load/

于 2012-11-12T18:23:29.747 回答
0

您需要使用客户端 javascript AJAX。查找并阅读有关 AJAX 请求的信息。

一旦您知道 AJAX 有效,您可以在点击按钮上调用 cart.php?runco​​mmand=xyz,在 cart.php 中您可以检查 $_GET['runco​​mmand'] == 'xyz' 是否然后执行 x。你会发现 jQuery javascript 库有很好的 AJAX 支持。

最好不要在一个 php 文件中包含所有内容,然后您可以出于不同目的向不同文件发出 AJAX 请求。例如,在您的情况下,listproducts.php。

于 2012-11-12T18:24:28.123 回答
-1

我想你正在寻找这样的东西:

<script type="text/javascript" language="javascript">
function showHide() {
var ele = document.getElementById("showHideDiv");
if(ele.style.display == "block") {
        ele.style.display = "none";
  }
else {
    ele.style.display = "block";
}
}
</script>

<form method="post" action="">
<p><input type="button" value="Show-Hide" onclick="return showHide();" /></p>
</form>
<div id="showHideDiv" style="display:none;">hello!</div>
于 2012-11-12T18:26:12.347 回答