php - 系统打卡 PHP, Ajax, Mysql

标签 php jquery mysql ajax

我正在尝试编写一些代码,使我的同事能够打卡上类/下类。 首先,他们选择他们出生的月份,然后出现该月份的所有名字。此数据是从数据库中检索的。

这一切都适用于这段代码(我从这个网站获得了大部分)-(index.html)(这部分有效,并将根据数据库中的月份显示正确的用户列表。

function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","show.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>

<body>
<form>
<h3 align="center">To clock in / out please select the month you were born</h3>
  <div class="wrapper">         
  <div class="row top-buffer">          
    <input class="btn col-md-2" value="January" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="February" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="March" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="April" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="May" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="June" type="button" onclick="showUser(this.value)">
    </div>
  <div class="row top-buffer">          
    <input class="btn col-md-2" value="July" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="August" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="September" type="button" onclick="showUser(this.value)">        
    <input class="btn col-md-2" value="October" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="November" type="button" onclick="showUser(this.value)">
    <input class="btn col-md-2" value="December" type="button" onclick="showUser(this.value)">
    </div>
  </div>
  </form>
<br>
<div id="txtHint"><b></b></div>  

但是,当他们选择他们的名字时,我需要它来调用一个 PHP 文件,该文件将那个名字写回链接的数据库。

显示.php -

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

$("#submit").click(function(){
var fullname=$("#submit").val();
$.ajax({
                    url:"insert.php",
                    type:"POST",
                    data:fullname,
                   success:function(data){
                       alert(data);
                       window.location.href = url;  
                   }
                });
                });
                });
</script>



</head>
<body>

<?php
$q = ($_GET['q']);

$con = mysqli_connect('x','x','x','x');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"xxx");
$sql="SELECT * FROM clock WHERE month = '".$q."'";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {

    echo "<div class=\"col-md-2\"><input type=\"Submit\" id=\"submit\" value= '". $row['fullname'] ."' name=\"submit\" ></div>" ;

}

mysqli_close($con);
?>
</body>

根据我的测试,按他们的名字这根本不是在调用 insert.php。更不用说发送名称值了。但是,它正在刷新页面。

许多代码是根据对不同问题的回答以及我有限的编码知识生成的。

如有任何帮助,我们将不胜感激。

最佳答案

使用 preventdefault();这将阻止默认表单操作或刷新页面。

  $("#submit").click(function(e){
    e.preventdefault()
    var fullname=$("#submit").val();
    $.ajax({
             url:"insert.php",
             type:"POST",
             data:fullname,
             success:function(data){
              alert(data);
              window.location.href = url;  
            }
           });
         });
     });

关于php - 系统打卡 PHP, Ajax, Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59093488/

相关文章:

javascript - 单击图像后在同一浏览器窗口中打开多个选项卡

java - 如何使用 Java 创建 MySQL 日志

php - 在 Symfony 项目上使用 "Class Table Inheritance"加入 Doctrine 太多

php - 如何使用索引使mysql查询速度更快

php - 在 MySQL 中调用 NOW() 函数

php - 从 PHP url 编码字符串中删除所有反斜杠

jQuery append() - 在我看来它应该可以工作,但不行

jquery - 如何给不同的div添加不同的事件

php - 将 PHP + MySQL 应用程序制作成适用于 Windows 的软件?

mysql - 启用 local-infile 以将数据从 rails 加载到远程 mysql