我有两个文件。 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
}
}
?>
答案 0 :(得分: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");
})
当您点击ID为“page.html”的链接时,该代码会将ajax.php中doit
的结果加载到ID为“products”的div中
答案 1 :(得分:0)
您需要使用客户端JavaScript JavaScript AJAX。查阅并阅读有关AJAX请求的信息。
一旦你知道AJAX工作,你可以在点击按钮上调用cart.php?runcommand = xyz,在cart.php中你可以检查$ _GET ['runcommand'] =='xyz'然后执行x。你会发现jQuery javascript库有很好的AJAX支持。
最好不要在一个php文件中包含所有内容,然后您可以为不同目的向不同文件发出AJAX请求。在您的情况下,例如listproducts.php。
答案 2 :(得分:-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>