dsfsw1233 2015-10-29 17:37
浏览 46
已采纳

在一个页面中有两个表单并设置会话

i have two file and two forms:

file 1:

<form name="name1" action="form2.php" method="post">
     <input <? $_session['define'] = 'value1' ?> type="submit" ...>
</form>
<form name="name2" action="form2.php" method="post">
     <input <? $_session['define'] = 'value2' ?> type="submit" ...>
</form>

As you see i have two forms and two different submit buttons but when i press each of them, the second value (the last) set to the $_session['define'] and in the second form i always have 'value2'.

  • 写回答

1条回答 默认 最新

  • du27271 2015-10-29 18:03
    关注

    You must post the data to PHP:

    <form name="name1" action="form2.php" method="post">
         <input name='v1' value='1' type="submit" />
    </form>
    <form name="name2" action="form2.php" method="post">
         <input name='v2' value='2' type="submit" />
    </form>
    

    In form2.php:

    <?php
    session_start();
    $_SESSION['value1'] = isset($_POST['v1'])?$_POST['v1']:0;
    $_SESSION['value2'] = isset($_POST['v2'])?$_POST['v2']:0;
    ?>
    

    Personally, I would make it one form and use JQuery/AJAX to POST the values.

    <form id="selectForm" name="name1" action="form2.php" method="post">
         <input id="v1" name='v1' value='1' type="submit" />
         <input id="v2" name='v2' value='2' type="submit" />
    </form>
    <script>
    // Requires JQuery
    $("#selectForm input[id^='v']").click(function(e){
        e.preventDefault();
        $.post(form2.php, { $(this).attr("name"): $(this).attr("value") }, function(){ alert("Selection Saved.) });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用