Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Submit Buttons 2

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I saw the postings here and elsewhere online about it but surely it can't be that difficult to have a form with two submit buttons of the same name and different values! However, I am very poor with arrays having just returned to PHP after years of disuse so can someone help? Because it is submitting to a third-party application, I can't really give the form buttons two different names. The form button images were originally like this:

Code:
<input type="image" name="action" value="UpdateItem" src="button1.jpg">
<input type="image" name="action" value="ProcessOrder" src="button2.jpg">

but I could not seem to be able to fetch any values from either one so I revised them to this:

Code:
<input type="image" name="action[UpdateItem]" src="button1.jpg">
<input type="image" name="action[ProcessOrder]" src="button2.jpg">

and viewing the raw output shows this:

Code:
Array
(
    [action] => Array
        (
            [UpdateItem] => 0
        )

)

and this for the other button:

Code:
Array
(
    [action] => Array
        (
            [ProcessOrder] => 0
        )

)

and I am trying to fetch it and create variables with this but it is not getting through so any help would be greatly appreciated:

Code:
switch (array_keys($_POST['action'])) {
	case 'AddItem':
		$CartAction = "AddItem";
	break;

	case 'DeleteItem':
		$CartAction = "DeleteItem";
	break;

	case 'UpdateItem':
		$CartAction = "UpdateItem";
	break;
	
	case 'ProcessOrder':
		$CartAction = "ProcessOrder";
	break;	
}
 
Hi there,

array_keys() always returns an array to.
So, if you are sure that $_POST['action'] has always exactly one element you can do something like:

PHP:
$all_keys = array_keys($_POST['action'])
switch ($all_keys[0]) {
	case 'AddItem':
		$CartAction = "AddItem";
	break;

	case 'DeleteItem':
		$CartAction = "DeleteItem";
	break;

	case 'UpdateItem':
		$CartAction = "UpdateItem";
	break;
	
	case 'ProcessOrder':
		$CartAction = "ProcessOrder";
	break;	
}



Best Regards,
Marcel

___
____
 
image controls do not submit their value. just their x,y coords. better to use a submit input and overlay an image if you must have image based controls (with css background or use the sliding-doors technique).

however you can still make it work using the name attribute as in your second example

Code:
if( is_array( $_POST['action'] ) ){
  switch ( key( $_POST['action'] ) ){

or keeping closer to your code (but less optimised)

Code:
if( is_array( $_POST['action'] ) ){
  switch ( current( array_keys( $_POST['action'] ) ) ){

however I'm very surprised that the raw output of the image control is simply a value of zero (or some other integer). i would have expected php to have split the x and y coords in to action[addItem_x] (say). I think this behaviour is probably a long standing bug that noone has bothered fixing as the use case to generate the bug is a bad one.
 
Both suggestions seem to work although with predamarcel's idea the submit button images have disappeared leaving only the alt text which I did not post above. I can't imagine what it might have to do with it since the form and the case conditional are nowhere near each other! The form, though, is actually inside of PHP even though I showed it as plain HTML so that clearly is causing the issue so I'm sure I can fix it if needed. On jpadie's comment, it does seem to be returning some seemingly-random integer but with his code, the buttons remain.

The button images, by the way, were simplified for this posting to regular images but they are actually created on the fly by PHP's GD library so I can't really move them to a style sheet.

Anyway, thank you to both of you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top