I am currently working on a web site whereI want to use images (buttons) to submit forms.

All is fine using the method below:
.......
echo "<form name=\"myform\" action=\"$PHP_SELF\" method=\"post\">";
echo "<input type=\"image\" name=\"imagename\" src=\"imagesrc\">";
echo "<input type=\"hidden\" name=\"submit\" value=\"somevalue\">";
echo "</form>";
........

The trouble begins when I use more than one image button within the form. I know that the variable submit is given the value 'somevalue' and is then passed back to $PHP_SELF, but what can I do if I have three buttons which should act logically as ORs , i.e. I have three choices and only want to select one. I will then do one of three things based on the choice made.

I know I can't use three different hidden variables because they will all receive the values placed in the 'value' tag.

Any ideas?

PS I'm not adverse to using PERL if I have to, but I would prefer not to have to resort to javascript.

Cheers

david

    • [deleted]

    try to use javascript :

    echo "<form name=\"myform\" action=\"$PHP_SELF\" method=\"post\">";
    echo "<input type=\"hidden\" name=\"submit\" value=\"\">";
    echo "<input type=\"image\" name=\"imagename\" src=\"imagesrc1\" onclick='document.myform.submit.value=1'>";
    echo "<input type=\"image\" name=\"imagename\" src=\"imagesrc2\" onclick='document.myform.submit.value=2'>";

    echo "</form>";

      Thanks vierme.
      I wanted to avoid javascript because it's too easy to turn off, but I have looked into the problem and think I have a working solution.

      If I use:
      echo "<form name=\"myform\" action=\"$PHP_SELF\" method=\"post\">";
      echo "<input type=\"image\" name=\"imagename1\" src=\"imagesrc\">";
      echo "<input type=\"image\" name=\"imagename2\" src=\"imagesrc\">";
      echo "<input type=\"hidden\" name=\"submit\" value=\"somevalue\">";
      echo "</form>";

      And then have the line:

      if (isset($imagename1_x))
      {
      do some code;
      }
      , then this should tell me which button has been pressed.

      Seems to work, but do you know if I have overlooked anything ?

      Many thanks

      David

        You don't have to get fancy; HTML 1.1 provides a solution for multiple post options in a form using images as buttons:

         
        print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\"><BR>\n";
        print "<INPUT TYPE=\"text\" NAME=\"myField\"><BR>\n";
        print"<INPUT TYPE=\"image\" SRC=\"/images/add_button.png\" NAME=\"additem\">\n";
        print"<INPUT TYPE=\"image\" SRC=\"/images/edit_button.png\" NAME=\"edititem\">\n";
        print "</FORM>";
        

        When you process this form, you check which button was pressed by checking the x or y offset that indicates the position of an image that was clicked (same mechanism as checking an image map click position):

        If ($_POST['additem_x'] != "") {
          print "add button was pressed";
        }
        else {
          print "edit button was pressed";
        }
        
          Write a Reply...