我正在尝试编写一些代码,使我的同事能够打卡上班/下班。首先,他们选择他们出生的月份,然后显示该月份的所有名称。此数据是从DB中检索的。
这一切工作与此代码(我从这个网站得到了大部分)- (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文件,该文件将该名字写回链接数据库。
show.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。更不用说发送name值了。然而,它正在刷新页面。
许多代码都是通过对不同问题的回答,以及我有限的编码知识而产生的。
任何帮助都将不胜感激。
发布于 2019-11-29 05:29:39
使用default ();这将阻止默认表单操作或刷新页面。
$("#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;
}
});
});
});https://stackoverflow.com/questions/59093488
复制相似问题