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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料