Page 1 of 1

Multiple Submit Buttons PHP Rookie

Posted: Sat Sep 09, 2006 9:44 am
by lonewolf1882
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am sure the solution here is simple... but I am not quite sure where to start my [b]Search[/b] in the forums

Within an HTML Form, I have an array that posts a string as well as an "EDIT"  button with it (type="submit" name="edit[]").
Upon pressing the "EDIT" button, I wish to have the textbox fill with the appropriate string in the array.

Code: Select all

<table>
<form method="POST" name="EditTest" action="<? $_SERVER['PHP_SELF']; ?>">


<?php
$array1 = array("Monkey", "Stork", "Kangaroo");
$EditIt ="";

//If the button is pressed, Fill the textbox with the given array value
    if (isset($_POST['edit']))  //Edit Database Value
	{
    	   for($i=0;$i<3;$i++)
    	   {
        	    if (isset ($_POST['hEdit'][$i]))
        	      {
        	         $Pressed =  $_POST['hEdit'][$i];
                         $EditIt = $array1[$i];
            	         echo "$Pressed <br>";
        	      }
    	   }

          echo<<<EOD
        <tr><td colspan="3">
        <input type="text" name="dogPrice" size="26" value="$EditIt">
        </td></tr>
EOD;

	}
    else
    {
    	echo<<<EOD
        <tr><td colspan="3">
        <input type="text" name="dogPrice" size="26" value="">
		</td></tr>
EOD;
    }

    for($i=0;$i<3;$i++)
    	{
                echo<<<EOD
                <tr><td>  $array1[$i] &nbsp;
        		<input type="submit" value="Edit" name="edit[]">
        		<input type="hidden" value="$i" name="hEdit[]">
        		</td></tr>
EOD;
		}

?>
</form>
</table>
With my poor code, When one of the submit buttons is pressed, all edit[] buttons are considered posted, so the textbox always ends up witht he last value in the array "Kangaroo".
I wish to figure out how to tell WHICH Edit[] Button was pressed so I can get the appropriate array value.

edit[1] = PRESSED;
array[1]=Stork;
textvalue=Stork;

I hope my yammering made sense!
-Dom


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Sep 11, 2006 4:02 pm
by kendall
lonewolf1882,
I hope my yammering made sense!
lol....i'm trying to grasp your concept....

I think you either need to remove the [] from the name replacing them wit unique names like edit1 and edit2

Posted: Mon Sep 11, 2006 9:10 pm
by Ambush Commander
Yep: there's no way to distinguish between the edit buttons any other way. You could also use the attribute "value" as in name="edit" and value="nameoffield"

Posted: Mon Sep 11, 2006 10:31 pm
by nickvd
From what I remember, the name='foo[]' "trick" is limited to php only (and perhaps others due to php's popularity) and is not part of the standards