【问题标题】:Unable to Store Session Variable On Form Submit无法在提交表单时存储会话变量
【发布时间】:2013-03-08 10:09:54
【问题描述】:

当我尝试将表单 a 提交到表单 b 并使用 javascript window.open 或表单标签本身的 action="" 属性将其加载到新页面时,一切都会正确触发,但在新页面加载时SESSION 变量未通过。但是,如果我取消 window.open 方法,提交表单并在浏览器中手动加载新页面,SESSION 变量确实会在表单 b 的新页面的输入字段中正确显示...

我想知道它是否与“return false;”有关。我在表单a上运行的onsubmit函数的一部分,或者如果在表单提交和新页面加载时,它没有在SESSION存储它们并将新页面加载到窗口之前设置SESSION值......它必须是其中之一那两个罪魁祸首。如果我不进行页面重定向,它就可以工作,所以我相信在提交后加载新页面时它实际上并没有存储 SESSION 变量......我怎样才能正确并成功地在新页面加载时存储 SESSION 以便它加载在表单 b 的新页面中正确吗?

索引文件(表格 A)

<?php
session_start();
if(isset($_POST['submit'])) {
    $_SESSION['cable_no'] = $_POST['cable_no'];
    $_SESSION['co_name'] = $_POST['co_name'];
    $_SESSION['prepping_team'] = $_POST['prepping_team'];
    $_SESSION['section_no'] = $_POST['section_no'];
    $_SESSION['tech_name_a'] = $_POST['tech_name_a'];
}
?>
<header>
<script type="text/javascript" language="javascript" src="/jquery/js/jquery-1.9.1.js">       </script>
<script type="text/javascript" language="javascript" src="js/scripts.js"></script>
<link href="css/style.css" type="text/css" rel="stylesheet" />
</header>
<form method="post" onsubmit="uploaddata(); return false;">
HTML CODE
<input type="submit" id="submit" name="submit" value="Continue" />
</form>

JS 文件

function uploaddata() { 

//Read all of the data from the page
for (eID in prepform) {
    prepform[eID] = document.getElementById(eID).value;
}
//Send to database upload function and verify IF checkboxes are checked
if(document.getElementById("a11").checked && document.getElementById("b11").checked && document.getElementById("c11").checked && document.getElementById("a12").checked && document.getElementById("b12").checked && document.getElementById("c12").checked) {
        upload_prepform();
    }else{
        alert("Please verify that you have checked the known check boxes.")
    }
}   
function upload_prepform() {
    $.ajax({
            type: 'POST',
            url: './php/upload_prepform.php',
            data: prepform,
            async: false,
            dataType: 'text',
            success: function() {
                alert("Thank you. Your Prep form has been submitted.");
            },

            error: function(jqXHR, textStatus, errorThrown) {
                    alert("Error... " + textStatus + "\n" + errorThrown);
            },
            complete: function() {
                window.location.replace("http://dev1.rs.idmyasset.com/gp21-dev/prep_b/");
                alert("Redirecting...");
            }
        });
    };

【问题讨论】:

    标签: php session


    【解决方案1】:

    文件必须以

    开头
    <?php
    session_start();
    ?>
    

    html代码后不能使用

    【讨论】:

    • 我编辑了我的帖子...我在服务器上的文件中正确,但我在此帖子上错误地发布了它。另外,除了我实际上必须在表单提交时加载新页面外,一切正常,但无论如何感谢您的快速回复。
    • return false 似乎是目前的罪魁祸首...有没有更好的方法来运行函数uploaddata();所以它会正确启动?
    • 不需要return false。你可以删除它。
    • 你有uploaddata();函数你能告诉我它的代码吗?
    • 当然,更新了我的问题的 JS 代码。 upload_prepform 实际上是 uploaddata 调用的函数。如果愿意,Uploaddata 会执行检查。我省略了很多代码,只是因为其中很多是用来向 SQL DB 提交数据的,所以我不想让大家感到困惑,但看起来我还是设法做到了哈哈
    猜你喜欢
    • 2016-09-16
    • 2016-08-18
    • 2015-11-17
    • 2021-08-13
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多