2013-07-19 09:07



I have a script which generates a table containing numerous rows and coloumns. On each end of a row, is a Submit button, whose name is one of an array generated on running the script, and goes like: delete_CN[0], delete_CN[1] etc.

While processing the form POST, I need to determine which Submit button was clicked. Currently, when I see a print_r or dump_vars, I see the following: ["delete_CN"]=> array(1) { [0]=> string(6) "Delete" }, which does not give the array index of the button that was pressed.

How should I best go about doing this? I need to find the array index of the submit button which was pressed, or another way to uniquely identify the row of the Submit button, to process the data. The original form is dynamically generated by reading a zonefile, and the elements on the same row are all arrays.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douyou1960 douyou1960 8年前

    the post data will always include all buttons but only the one which was clicked will have a value. this way you can determine easily which button was clicked by checking which value in the button array is not empty. (The value will be the text on the button as that text is defined in the value attribute).

    点赞 评论 复制链接分享
  • dsshsta97935 dsshsta97935 8年前

    Be aware that a form can also be submitted using the ENTER key. In case this happens the browser handles it like the first button in the form was clicked.

    Obviously, pressing ENTER when focus is on a submit-button sends the form using this currently focused button.

    点赞 评论 复制链接分享
  • dsf22567 dsf22567 8年前

    In HTML/PHP you could make each row in the table a seperate form, and add a

    <input type="hidden" name="which_clicked" value="delete_CN[0]">

    then look for $_POST['which_clicked']

    or you could use jQuery and have a which value is inserted based on which submit button is clicked.

    点赞 评论 复制链接分享
  • dqbr37828 dqbr37828 8年前

    What I do, I create an array of input type hidden, with "unique value", and those value we assigned to each button. such as input type = "hidden" name = "txtID[]" id = "txtID[]" value = "15618" and for the button input type="submit" name="CityAdd15618" id="CityAdd15618" value="+"

    then when submit, you just need to loop

    for($lCtr1=0; $lCtr1<$lTtl; $lCtr1++)
        if (isset($_POST['CityAdd' . $_POST['txtID'][$lCtr1]]))
    echo "ttl:" . $lCtr1 . "<br>
    点赞 评论 复制链接分享