How to post variables to a new page

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
Perlyman
Forum Newbie
Posts: 2
Joined: Thu Sep 14, 2006 5:16 pm

How to post variables to a new page

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: How to post variables to a new page

Post 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;
}
Perlyman
Forum Newbie
Posts: 2
Joined: Thu Sep 14, 2006 5:16 pm

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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>
Post Reply