dsirr48088 2012-11-27 05:43
浏览 23

使用PHP解决动态表单值问题

I have a loop in a form for dynamically adding/removing items, however, only the last item is removed every time. I can add items just fine though.

I can't figure out why no matter which items I try to remove from both add_friends and register_tasks, ONLY the last item is removed. When I echo $_POST['task_name'] it's always the last item on the list, never the one I selected(unless of course I select the last item).

Here is the form

 <center><h2>Create Event</h2></center>
        <form method="post"> 
        <?
        $form = new common_functions();

        if($form->check_saved_forms($my_username, "event") == TRUE){
            $this->existing_forms(); //allow the user to select existing forms that they have created
       }

        ?>               
        <tr><td>Title:</td><td><input type="text" width="20%" name="event_title" value="<? echo $form_data[0]; ?>" /></td></tr>
        <tr><td>Details:</td><td><input type="text" width="20%" name="event_details" value="<?echo $form_data[1]; ?>" /></td></tr>
        <tr><td>Date:</td><td><input type="text" width="20%" name="event_date" id="event_date" value="<? echo $form_data[2]; ?>" /></td></tr>
        <tr><td> <input type="submit" name = "submit_event" id = "lOBut" value="Create Event" /></td></tr>  
        </table>

    <?
    //this is the section giving me trouble
    $form->add_friends($added_friends); 
    $form->register_tasks($tasks,$nums); 
    ?>
    </form>

Both add_friends and register_tasks dynamically add and remove friends/tasks by a textbox, and a list of the added items are shown. users can remove items, however, right now only the last item is removable and I can't figure out why.

Here is register (add_friend is the same, but labeled differently)

function register_tasks($tasks,$nums){
    <table>    
    <td><input type="textbox" size="50" name="register_description"/> </td></tr>
    <td><input type ="textBox" name="register_num" /> </td>
    <td><input type="submit" name="add_register_tasks" value="Add"/></td></tr>
    </table>
    <?
    if(count($tasks) > 0){
        ?>
        <table>
        <tr><td><b>Registration Item Description</b></td><td><b># Of Available Spots</b></td></tr><br>
        <?
        foreach (array_combine($tasks, $nums) as $task => $task_num){
            ?>
            <tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
            <td><input type="submit" name="remove_task" value="Remove"/></td>
            <td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
            <td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>

            <?
        }
        ?></table><?
    }
  • 写回答

1条回答 默认 最新

  • douliu8327 2012-11-27 06:50
    关注

    You need to place the form tag inside the loop. Right now what happens is all the records are displayed in a single form and no matter what item you choose, it takes only the last value. Another alternate solution would be to use GET method and pass the number/name as query string.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?