Page 1 of 1

Amending function so that it runs automatically

Posted: Tue Jan 05, 2010 9:40 am
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

Re: Amending function so that it runs automatically

Posted: Tue Jan 05, 2010 10:28 am
by AbraCadaver
We probably need to see the switch statement to be able to tell.

Re: Amending function so that it runs automatically

Posted: Tue Jan 05, 2010 4:42 pm
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.