isset函数不起作用

时间:2016-06-27 07:47:57

标签: javascript php html mysql

下面我插入了两个表格

<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php
session_start();
$name = $_SESSION["name"];
echo $name;
if(isset($_POST["starttime"])){
$date = date('Y-m-d');
require "database.php";
$sql = "SELECT name, ddate FROM karthick.date WHERE name = '$name' AND ddate = '$date'";
$result = $conn->query($sql);
if($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        $n = $row["name"];
        $d = $row["ddate"];
    }
}
if($n == $name && $d == $date){
    echo'<script>
    alert("Time already registered");
    </script>';
}else{
    header("location:http://localhost/karthick/attendance/datedata.php");
    exit;
}
}
?>
</form>
<form action = "datedata.php" method="post">
<button name="endtime" class="endtime">Endtime</button>
</form>
<form action="user.php" method="post">
<button name= "logout" class="logout">Logout</button>
</form>
</body>
</html>

在上面的表单中,当点击starttime时它会获取日期,如果日期已经注册,它会用数据库检查它。如果日期尚未注册,它将移动到我在标题中给出的下一个表单:链接。

secode form

<?php
require 'database.php';
session_start();
$name = $_SESSION["name"];
echo $name;
if(isset($_POST["starttime"]))
{
    $date_clicked = date('H:i:s');
    echo $date_clicked;
    $date_clicked1 = date('Y-m-d');
    echo $date_clicked1;
    $sql = "INSERT INTO karthick.date (name, ddate, starttime) VALUES ('$name', '$date_clicked1', '$date_clicked')";
    if($conn->query($sql) === TRUE){
        echo "inserted";
    }else{
        echo "error";
    }
}elseif(isset($_POST['endtime']))
{
    $date_clicked = date('H:i:s');
    $date_clicked1 = date('Y-m-d');
    $sql = "UPDATE karthick.date SET endtime='$date_clicked' WHERE name = '$name' AND ddate = '$date_clicked1'";
    if($conn->query($sql) === TRUE){
        echo "inserted";
    }else{
        echo "error";
        echo $name;
    }
}
$conn->close();
?>

所以我遇到的问题是当它移动到第二种形式时它只获取名称而它不会获取日期和时间。所以请帮助我。

1 个答案:

答案 0 :(得分:1)

您的按钮没有分配值,这将在$_POST请求中跳过它:

<button name="starttime" class="starttime" value="insert value here">starttime</button>

此外,您错过了表单action=""的目标(它将默认为原始页面,但最好声明它)。

然后,当您通过标题声明

更改为第二个表单时
 header("location:http://localhost/karthick/attendance/datedata.php");

这不再是$_POST请求,您可以通过ajax加载此内容并添加帖子值,否则传递它们。当然,$_SESSION变量仍然存在。