dsfsw1233 2015-10-29 09: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 10: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部