Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:53:24+00:00 2026-06-14T04:53:24+00:00

I have two files. index.php and cart.php. All my functions, connection to database, etc.

  • 0

I have two files. index.php and cart.php. All my functions, connection to database, etc. are located at file cart.php. There are functions that I want to call out if user clicks on a HREF link.

my 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>

Then, How can I make that a HREF link calls out a specific function(which is located in cart.php) and displays it inside a div tag(in my case, in div id=”products”)?

It’s probably easy, I am just a beginner.

Here’s my cart.php just in case.

<?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


    }
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:53:25+00:00Added an answer on June 14, 2026 at 4:53 am

    If you want a php function to run when you do something on the client, you have to make an AJAX request that calls that function, then you insert the HTML dynamically when your AJAX request returns.

    Remember that PHP only generates text that is sent over the wire as HTML,CSS,JavaScript,JSON, XML,… There is no way for PHP to interact directly with your page, it can only generate new content

    Here’s a basic AJAX example you could use; I’ll use jQuery and some simplified PHP code

    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");
    })
    

    That code loads the result of doit in ajax.php into the div with id “products” when you click on the link with ID “page.html”

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files: index.php and cart.php In cart.php I have few three functions
I have two files php (gettable.php and index.php) the index file display the result
The setup: I have a standard .php file (index.php) that contains two includes, one
I have two files 1 - index.php 2 - main.php index.php call to main.php
i have two files:(localhost/template/) index.php template.php each time when i create an article(an article
I have two files client.php and server.php. The client file send a HTTP request
I have two files: index.php /lib/user.php Index contains the form: <div class=<? echo $msgclass;
So I have two files: index.php and query.php. The problem is, I don't know
I have two files php(index.php & data.php), the first send data to the second,
I have a scenario where I have two files, one is index.php and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.