Forms: How do I use 2 buttons for sending the data each to different pages?
For me the big problem is action='foo.php'
Unless I am mistaken, any submit button would send the form to foo.php by POST, so I would need a second form to have an alternate action page.
I made a table to show the user's contents in the shopping cart, with a text box for them to alter their item quantities. The submit button for the form is the checkout button, but how would I make another button called update to send the form to a different page to change the shopping cart details if they altered their quantities?
About the only way I can figure is to query the shopping cart table again and make a second form but not show anything other than the submit button, which would be the second button.
What do you guys think? I've been beating my brain over this for two days now.
Any help would be appreciated.
Diode
Junior Poster in Training
70 posts since Jan 2005
Reputation Points: 50
Solved Threads: 0
I'm sorry, can you elaborate on that just a little more? I understand that I am supposed to use javascript, but I'm kind of confused. I have to make another form anyway is what you're saying?
Thank you for your reply.
Diode
Junior Poster in Training
70 posts since Jan 2005
Reputation Points: 50
Solved Threads: 0
If they have javascript disabled, you'll never be able to update the quantites.
Think of adding an item, the same as changing quantities, overall it is a change.
You could repost the quantities and reflect them that way along with any new additional items.
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
If they have javascript disabled, you'll never be able to update the quantites.
Think of adding an item, the same as changing quantities, overall it is a change.
You could repost the quantities and reflect them that way along with any new additional items.
Thanks for both of you trying to help me. dickersonka, I thought about that javascript thing, but then I also realized that many sites on the internet DO use javascript, and if a person had it disabled, there would be a vast number of sites they couldn't view or browse correctly, so basically the user is told, enable javascript or you can't use this site. Cookies are the same way. I'm not arguing with you, you just presented an opportunity for me to ask a question that I've been wondering for a long time, and that is, how can you possibly cater to a user that has everything disabled? I mean almost every site on the web now uses javascript. This was the very reason I never bothered trying to learn javascript until now: the user might not even have it enabled, so why script in it? In a way I agree with you, but I was just wondering that.
As far as reposting, do you mean to make the page its own action page and re-POST the data back to the same page? If not, I guess I am totally lost.
Thank for your patience, guys.
diode
Diode
Junior Poster in Training
70 posts since Jan 2005
Reputation Points: 50
Solved Threads: 0
i use javascript for client side validation, not for functionality
lets say for a field that will be submitted needs to be a minimum of 8 characters, they type 4, and i can use javascript to warn them, but also validate server side that the input is 8 chars
this will ensure that your data is still valid, but adds an extra element just for look and feel on client side
with javascript enabled or disabled, this will work, just whether the client side gets a message before postback
yes i would repost the data back to itself, if its a button for submitting, read back the data, then redirect to final cart page with your updated data, otherwise stay on this page
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
Adding two submit buttons to the form allows you to send different GET or POST params depending on which submit button was pressed.
If you give a name and value for each Button, then in the POST, you'll get the name=value of the button set.
Eg: Your buttons are:
<input type="submit" name="action" value="Update" />
<input type="submit" name="action" value="Checkout" />
Then when the user clickes Update, you get:
$_POST['action'] = 'Update';
If the user clicks Checkout:
$_POST['action'] = 'Checkout';
This can be used by PHP to carry out the necessary task without involving JavaScript on the client side. It doesn't give you separate pages, just one page, but different values for the parameter named by the submit buttons.
If you want separate pages to handle the request, you can involve a "controller" into your PHP code, that will include() the correct file depending on the submit button clicked.
eg:controller.php
if ($_POST['action'] = 'Update') {
require_once('update.php');
} else if ($_POST['action'] = 'Checkout') {
require_once('checkout.php');
} else {
echo 'Action not specfied';
}
This is all server side however, so the client/browser still sees the url as controller.php?action=update or controller.php?action=checkout
The benefit is that you don't need JavaScript, and so will support http clients that dont' have JS or have JS turned off.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
See also the tag. It has better support for this as it allows a label different from the value attribute.
http://www.w3.org/TR/html401/interact/forms.html#h-17.5
eg:
<button name="action" value="update">Update Cart</button>
<button name="action" value="checkout">Checkout Now!</button>
With you have to use the value attribute as the label. This makes the server side code dependent on a display item, rather then data/logic (Your model is dependent on the view if worded in the MVC pattern/paradigm) not the best thing.
eg:
If you wanted the checkout button to read "Checkout Now", you'd need to change the server side code to test the value to be "Checkout Now" if you use an .
If you use , you can rename the visual aspect, while keeping the controller logic the same.
ps: you could always make the dependency a variable that PHP will understand and write to HTML as well: eg:
define('checkout_label', 'Checkout Now');
if ($_POST['action'] = checkout_label) {
require('checkout.php');
}
but I still think is better.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101