user1899891
user1899891

Reputation: 259

How would I get the value from a dynamic $_POST variable name?

Basically, I have a website that dynamically creates submit buttons. Ex:

input type="submit" name="test1"

input type="submit" name="test2"

and so on....

How would I find out which button was pressed (test1, test2, test3, etc)?

I know you can create multiple forms instead with a hidden value but I would rather just have 1 giant form with multiple submits.

Edit: The buttons are created dynamicly so I don't know how many there will be. It could be test1-test55 so I need to create something that looks at all the possibilities automatically.

Upvotes: 0

Views: 5441

Answers (5)

Steven Liao
Steven Liao

Reputation: 3787

If you can change your button generation code, you have a few options.

If your buttons are generated consecutively (1,2,3,...), you can use PHP's magic powers as documented here (scroll down to "How do I get all the results from a select multiple HTML tag?"):

for ($i = 0; $i < MAX; $i++){
    echo '<input type="submit" name="test[]"'." more button code />";
}

If your button numbers come from an array, you could do:

for ($arr as $i){
    echo '<input type="submit" name="test['.$i.']"'." more button code />";
}

In either of these ways, you can then access the form elements in an array. Since these are buttons, and only one of them can be responsible, you can use this clever method to get the posted value.

$the_posted_button_value = reset($_POST['test']);

Upvotes: 0

anon
anon

Reputation:

Store the $_POST array in a different variable, reverse the array, and use key() to get the first key (which is the submit button):

The following should work:

<?php

if(!empty($_POST)) {
    $array = $_POST;
    $array = array_reverse($array);
    $key = key($array); // get first key
    echo $key;
}

?>

<form action="" method="post">
    <input type="text" name="foo" />
    <input type="submit" name="submit1" />
    <input type="submit" name="submit2" />
    <input type="submit" name="submit3" />
</form>

For example, if you submit a value and click any submit button, it should echo the name attribute of the submit button that was clicked. The above solution will work regardless of the number of submit buttons in your form.

Upvotes: 0

Sumurai8
Sumurai8

Reputation: 20737

If you would submit a simple form like this:

<form>
  <input type="text" id="icebear" value="tickle" />
  <input type="submit" id="doit" value="SUBMIT" />
</form>

The receiving page will get two variables:

  • icebear (with value "tickle")
  • doit (with value "SUBMIT")

If you have no clue what the name of the submit-button might be, remember that $_POST is an array. You can use foreach on it. You can possible filter out the submitbutton on a common prefix that way.

foreach( $_POST as $key => $value ) {
  echo "{$key} was submitted with value {$value}<br/>";
}

Upvotes: 1

Moeed Farooqui
Moeed Farooqui

Reputation: 3622

<form action ="" method="post">
    <input type="submit" name="test1" />
    <input type="submit" name="test2" />
    <input type="submit" name="test3" />

</form>

<?php
if(isset($_POST['test1'])){
    echo 'first button is pressed';
}elseif(isset($_POST['test2'])){
    echo 'Second button is pressed';
}

elseif(isset($_POST['test3'])){
    echo 'Third button is pressed';
}

    ?>

Upvotes: 0

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

you can test which submit is sended with an if or switch statement like this:

if(isset($_POST['test1'])){
    //form test1 submitted
}
else{
   if(isset($_POST['test2'])){
    //form test2 submitted
   }
}

you can check in all the $_POST key like this

foreach($_POST as $key => $value){
   if(substr($key,0,4) == 'test'){
      $form_submitted = substr($key,5); //retrieve number of submitted test
   }
}

Upvotes: 1

Related Questions