我有两个文件。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 />£'.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 />£'.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 />£'.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.' @ £'.number_format($get_row['price'], 2).' = £'.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: £'.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
}
}
?>