Amending function so that it runs automatically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

Amending function so that it runs automatically

Post by slaterino »

Hi,
I have a function that runs whenever a form is submitted using $_POST. However, I want this function to run when the page first loads also as I have changed slightly the way the form works and I believe this work exactly how I want it.

The function is:

Code: Select all

    case 'update':
    if ($cart) {
        $newcart = '';
        foreach ($_POST as $key=>$value) {
            if (stristr($key,'qty')) {
                $id = str_replace('qty','',$key);
                $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                $newcart = '';
                foreach ($items as $item) {
                    if ($id != $item) {
                        if ($newcart != '') {
                            $newcart .= ','.$item;
                        } else {
                            $newcart = $item;
                        }
                    }
                }
                for ($i=1;$i<=$value;$i++) {
                    if ($newcart != '') {
                        $newcart .= ','.$id;
                    } else {
                        $newcart = $id;
                    }
                }
            }
        }
    }
    $cart = $newcart;


How can I change this so that it runs automatically and not reliant on the form.

This is also some of the code from the form as this might be helpful:

Code: Select all

<form action="basket.php?action=update" method="post" id="cart">
<input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" />
<button type="submit">Update cart</button>
</form>
If anyone can help it would be greatly appreciated! I am so close to completing this script but keep encountering things that I never foresaw!

Thanks!
Russ
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Amending function so that it runs automatically

Post by AbraCadaver »

We probably need to see the switch statement to be able to tell.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Amending function so that it runs automatically

Post by pickle »

Put the code that runs in a function. In your case(), call the function. Call the function when the page is initially loaded as well.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply