Page 1 of 1

How to post variables to a new page

Posted: Thu Sep 14, 2006 5:26 pm
by Perlyman
Hello - I am new to the php world, but learning quickly.

I am working on a custom shopping cart page, index.php.

The form contains:

<FORM METHOD=POST ACTION="index.php">

In the form I have 'onchange="this.form.submit()" for the qty textbox's, so when the users enters or changes a qty the form submits and refreshes with a (php) calculated price based on quantity.

The form works great , but I cannot figure out how to add a button on the form to POST the variables to a new page.

I have tried adding a sub-form inside of the form with the cart button using

<FORM METHOD=POST ACTION="cart.php">

but it seems to ignore the cart.php, and if I add the button outside the form I lose all of the variables?

Any tips or suggestions appreciated -

Rob

Re: How to post variables to a new page

Posted: Thu Sep 14, 2006 5:36 pm
by Luke
Perlyman wrote:In the form I have 'onchange="this.form.submit()" for the qty textbox's, so when the users enters or changes a qty the form submits and refreshes with a (php) calculated price based on quantity.
Until you learn a little more and can apply some kind of ajax here I'd just let the user update the form via an "update" link or button. I would be annoyed if every time I changed something on a form the page reloaded.
Perlyman wrote:The form works great , but I cannot figure out how to add a button on the form to POST the variables to a new page.

I have tried adding a sub-form inside of the form with the cart button using

<FORM METHOD=POST ACTION="cart.php">

but it seems to ignore the cart.php, and if I add the button outside the form I lose all of the variables?

Any tips or suggestions appreciated -

Rob
You can't use "sub-forms"
Use a seperate submit button...

Code: Select all

<insert type="submit" name="submit" value="Submit"><insert type="submit" name="submit" value="Update">
Then check for which one was clicked like this:

Code: Select all

switch($_POST['submit']){
	case "Update":
	    // Do update stuff
		break;
	default:
	    // Do submit stuff
		break;
}

Posted: Thu Sep 14, 2006 5:52 pm
by Perlyman
Thanks for the help TNSG.

I agree the onchange re-submitting is annoying, I originally had an 'Add to Cart' button but the client insisted on 'automatic' updating of the price.

But I'll work on him some more....

So if I use:

Code: Select all

switch($_POST['submit']){ 
        case "Update": 
            // Do update stuff 
                break; 
        default: 
            // Do submit stuff 
                break; 
}
Can you point me in the direction of what syntax to use to call another page inside of php (and send the variables).

Thanks,

Rob

Posted: Thu Sep 14, 2006 5:58 pm
by Luke
Just to give you an idea...

Code: Select all

<?php
if(isset($_POST['posted'])){
    switch($_POST['submit']){
            case "Update":
		// Do update stuff
                echo "Updated";
                break;
            default:
                // Do submit stuff
		echo "Submitted";
                break;
    }
}
?>
<form method="post" action="#">
<input type="hidden" name="action" value="posted">
<input type="submit" name="submit" value="Submit"><input type="submit" name="submit" value="Update">
</form>