form with multiple image inputs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • inonzuk

    form with multiple image inputs

    Hi everyone, I have a problem that I cannot seem to find a solution.
    I'd appreciate any insight on it.

    I have a form with inside a dynamically created table of Images and 3
    inputs of type image:

    echo "<form method=post
    action='http://localhost/processplay.php ?$counter' >";
    echo "<table align=center border=10>";
    $counter = 1;
    for($i=0;$i<5;$ i++) {
    echo "<tr>";
    .....
    echo "<img src=$source width=30 height=30>";
    ......
    echo echo "<input name=$counter type=image height=30 width=30
    src=$source>"
    ......
    .....


    how can I pass to the processplay.php (the action of this form) which
    input was pressed? (remember that the name of the input is unknown to
    me before the form is created so I cannot write it explicitly).

  • Janwillem Borleffs

    #2
    Re: form with multiple image inputs

    inonzuk wrote:[color=blue]
    > how can I pass to the processplay.php (the action of this form) which
    > input was pressed? (remember that the name of the input is unknown to
    > me before the form is created so I cannot write it explicitly).
    >[/color]

    <?php

    echo "<form method=post action='process play.php' >";
    ....
    for($i=0; $i<5; $i++) {
    echo "<img src=$source width=30 height=30>";
    echo "<input name=counter_$i type=image
    height=30 width=30 src=$source>";
    }

    ?>

    Now, when one of the buttons is pressed, the $_POST array will contain one
    of the following keys:

    counter_0.x
    counter_1.x
    counter_2.x
    counter_3.x
    counter_4.x

    If you want to use another name, make sure you are using a unique prefix, so
    you can do something like:

    foreach ($_POST as $key => $value) {
    if (0 === strpos($key, "prefix_")) {
    // Do stuff
    }
    }

    JW



    Comment

    • inonzuk

      #3
      Re: form with multiple image inputs

      problem solved.

      JW, thanks alot for your help .

      Comment

      • inonzuk

        #4
        Re: form with multiple image inputs

        another small question:

        in my $_POST array I now have the value of (for example) "[img_2_x] =>
        16".

        I'm trying to loop as follows:

        for($i = 1 ; $i <= 25 ; $i++){
        //if this condition is true we know which image was clicked.
        if($_POST['img_$i_x']){
        echo ("<h3><br>Th e goal at input image number $i was
        selected.");
        }
        }

        this does not work because my syntax for "$_POST['img_$i_x']" is not
        correct.
        how do I write this condition in a correct way? (i'm trying to print to
        the screen the number of the image that was clicked).

        thanks in advance...

        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: form with multiple image inputs

          inonzuk wrote:
          <snip>[color=blue]
          > this does not work because my syntax for "$_POST['img_$i_x']" is not
          > correct.[/color]




          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          Working...