我有一个带有几个按钮的页面,这些按钮的值和名称都是从数据库中检索的。我试图在单击的任何按钮上运行insert查询,到目前为止,我的代码如下:
<?php
$sqlGetIllness = "SELECT * FROM illnissesandconditions ";
$resultGetIllness = $conn->query($sqlGetIllness);
while ($rowGetIllness= mysqli_fetch_array($resultGetIllness)){
echo "<div class='col-md-3'style='margin-top:20px;'><button onclick='insert(".$rowGetIllness['illness'].");' class='button button1' style=' color:white;' value='".$rowGetIllness['illness']."'>".$rowGetIllness['illness']."</button></div>";
}
function insert($value) {
$value='';
$sqlGetId = "SELECT commonID from common group by commonID DESC LIMIT 1 ";
$resultGetId = $conn->query($sqlGetId);
$r=mysqli_fetch_array($resultGetId);
$id=$r['commonID'];
$sqlGetIllness = "INSERT INTO medicalrecords (CommonID,Medical_Condition) VALUES (".$id.",'".$value."')";
$resultGetIllness = $conn->query($sqlGetIllness);
}当我在浏览器中检查函数时,传递给onclick中的函数的值是正确的,但是什么也不会发生。我已经有数据库连接了,有什么问题吗?在php中这样做可以不刷新页面吗?还是我需要像AJAX那样使用客户端lang?请注意,我从未在AJAX中工作过。
新编辑:
<script>
$("button").click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
data: {
condition: $(this).val(), // < note use of 'this' here
},
success: function(result) {
alert('Condition Inserted!');
},
error: function(result) {
alert('error');
}
});
});
</script>解决方案:我得到了解决方案,在编写脚本之后,我在页面顶部检索了变量值。
if (isset($_POST['condition'])) {
$value=$_POST['condition']; }在$_SERVER['REQUEST_METHOD'] == 'POST' )中插入值,当单击任何按钮时,我的下一步是将单击的按钮设置为背景色。
发布于 2017-12-22 09:24:00
解决方案是在Solution下发布的,这是我第一次尝试ajax,它确实工作了,给了按钮一个id,并通过this.val获取了它的值(单击的任何按钮)并通过post发送,检索并使用了insert查询变量中的值。
https://stackoverflow.com/questions/47933315
复制相似问题