单击提交按钮时,php会话无法使用表单

时间:2015-02-12 12:28:16

标签: javascript php html forms

我很快就要参加考试了。我坚持参加会议。在大多数情况下,它对我不起作用。无论我尝试过什么。它曾经为我工作过一次,但刷新页面之后,我从未见过它有效。我的样品:

qwer.php:

<?php
session_name('Private'); 
session_id('TEST');
if(isset($_POST["Submit"])){
session_start();
$_SESSION['counter']=5;
$_SESSION["lolvalue"]=isset($_POST["imie"]);
}
?>
    <html>
    <head>
    <title>Login</title>
    </head>
    <body>
    <form name="myform" method="post" action="qwew.php" onsubmit="return validateform();">
    <input type="text" name="imie"><br>
    <input type="submit" name="Submit">
    </form>
    <a href='qwew.php'>Link to protected file</a>
    </body>
    </html>

<script type="text/javascript">function validateform(){
checkit=/^[A-Z]{1}[a-zA-Z0-9]{7,16}$/;
if(!(myform.imie.value.match(checkit))){alert("blabla...bla");
return false;}
}
</script>

qwew.php

<?php
session_name('Private');
session_id('TEST');
session_start();

echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];

session_destroy();

?>

问题是表单和重定向到另一个页面应该通过单击提交来完成。请帮我弄清楚我错在哪里。 我也忘了使用XAMPP,session.use_cookies和session.use_trans_sid已启用

3 个答案:

答案 0 :(得分:1)

永远不会执行此代码:

if(isset($_POST["Submit"])){
    session_start();
    $_SESSION['counter']=5;
    $_SESSION["lolvalue"]=isset($_POST["imie"]);
}

这是因为您的HTML表单正在向POST文件发送qwew.php个请求而不是qwer.php

答案 1 :(得分:1)

qwer.php

<?php
    session_start();
    if(isset($_POST["Submit"])) {
        $_SESSION['counter']=5;
        $_SESSION["lolvalue"]=isset($_POST["imie"]);
        header('Location:qwew.php');
        die(); 
    }
?>

<html>
<head>
<title>Login</title>
</head>
<body>
<form name="myform" method="post" action="qwer.php" onsubmit="return validateform();">
    <input type="text" name="imie"><br>
    <input type="submit" name="Submit">
</form>
<a href='qwew.php'>Link to protected file</a>
</body>
</html>

qwew.php

session_start();
if(isset($_SESSION["lolvalue"]) && isset($_SESSION["counter"]))
 echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];
else {
   header('Location:qwer.php');
   die(); 
 }

答案 2 :(得分:0)

qwew.php

<?php
session_start();
if(isset($_POST["Submit"])){
    $_SESSION['counter']=5;
    $_SESSION["lolvalue"]=isset($_POST["imie"]);

    echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];
}
?>
    <html>
    <head>
    <title>Login</title>
    </head>
    <body>
    <form name="myform" method="post" action="qwew.php" onsubmit="return validateform();">
        <input type="text" name="imie"><br>
        <input type="submit" name="Submit">
    </form>
    <a href='qwew.php'>Link to protected file</a>
    </body>
    </html>

<script type="text/javascript">function validateform(){
checkit=/^[A-Z]{1}[a-zA-Z0-9]{7,16}$/;
if(!(myform.imie.value.match(checkit))){alert("blabla...bla");
return false;}
}
</script>