isset函数不起作用

isset函数不起作用

下面我插入我的两个表格

<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>

在上面的表格中,当单击开始时间时,它将获取日期,并与数据库一起检查日期是否已注册。如果日期尚未注册,它将移至我在header:link中给出的下一个表格。

密码表
<?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();
?>

所以我面临的问题是,当它移至第二种形式时,它只会获取名称,而不会获取日期和时间。所以请帮我

最佳答案

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

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

另外,您也缺少action=""表单的目的地(默认为原始页面,但最好声明它)。

然后,当您通过标题语句更改为第二种形式时

 header("location:http://localhost/karthick/attendance/datedata.php");

不再是$_POST请求,您可以通过ajax加载此请求并添加post值,否则将其传递。当然,$_SESSION变量仍然保留。

关于javascript - isset函数不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38048465/

10-11 03:22