[英]aJax update a specific row in sqlite and php using a button
I've got a table that lists values inputted by a user, with 2 buttons on the side to remove or to mark completed. 我有一个表格,列出了用户输入的值,侧面有2个按钮可以删除或标记完成。 On the page the table is visable, there are 3 tabs, we will call these Tab1, Tab2, and Tab3 在页面上该表是可见的,有3个选项卡,我们将调用这些Tab1,Tab2和Tab3
Each tab has a table (As described above) with information about a specific type of entry. 每个选项卡都有一个表(如上所述),其中包含有关特定类型条目的信息。
These buttons are simple <a href>
links, so when clicked they reload the page. 这些按钮是简单的<a href>
链接,因此在单击时会重新加载页面。 This is a problem because the users view is refreshed and it takes the tabs back to the default tab, and is also an inconvenience when trying to mark several entries. 这是一个问题,因为用户视图被刷新并且它将标签返回到默认选项卡,并且在尝试标记多个条目时也是不方便的。
I would like to make these buttons send Ajax requests to another page to process the data. 我想让这些按钮将Ajax请求发送到另一个页面来处理数据。 The only problem is, I am not really sure how to make the ajax call. 唯一的问题是,我不确定如何进行ajax调用。
This is what I have right now 这就是我现在所拥有的
My buttons 我的按钮
echo "<td class='td-actions'>";
echo " <a href='?complete=".$row['uniqueID']."' class='btn btn-success btn-small'>
<i class='btn-fa fa-only fa fa-check'> </i>
</a>
<a href='?remove=".$row['uniqueID']."' class='btn btn-danger btn-small'>
<i class='btn-fa fa-only fa fa-remove'> </i>
</a>";
echo "</td>";
There is one called Complete, and one called Remove. 有一个名为Complete,一个名为Remove。
When either of these are pressed, it currently reloads the page which triggers a few php if statements. 当按下其中任何一个时,它会重新加载触发一些php if语句的页面。
if(isSet($_GET['remove'])) {
$sql = "DELETE from rl_logged where uniqueID='".$_GET['remove']."';";
$ret = $db->exec($sql);
echo "<meta http-equiv='refresh' content='0;index.php' />";
}
if(isSet($_GET['complete'])) {
$sql = "UPDATE rl_logged set complete=1 where uniqueID='".$_GET['complete']."';";
$ret = $db->exec($sql);
echo "<meta http-equiv='refresh' content='0;index.php' />";
}
These are relatively simple functions. 这些是相对简单的功能。 My problem is that I do not know javascript very well. 我的问题是我不太了解javascript。
Any help would be much appreciated. 任何帮助将非常感激。
the javascript that I have come up with is this 我想出的javascript就是这个
$(document).ready(function() {
$('#markComplete').click(function() {
var input = input = $(this).text()
$.ajax({ // create an AJAX call...
data: {
onionID: input,
},
type: 'POST', // GET or POST from the form
url: 'pages/ajax/markCompleteRL.php', // the file to call from the form
success: function(response) { // on success..
refreshAllTabsWithFade();
}
});
});
});
using this button 使用此按钮
<div name='markComplete' id='markComplete' class='btn btn-success btn-small'>
<i class='btn-fa fa-only fa fa-check'></i>".$row['uniqueID']."
</div>
But, while inspecting with firebug, this seemed to work ONCE, but now the button doesn't do anything. 但是,在用firebug进行检查时,这似乎可以起作用,但现在按钮没有做任何事情。
I tried again this morning, the button presses and the first time it sends this post, then the button doesn't do it again - even on page reload. 我今天早上再次尝试,按下按钮并且第一次发送此帖子,然后按钮不会再次执行 - 即使在页面重新加载时也是如此。
I was able to get it to work with the following: 我能够使用以下内容:
javascript: JavaScript的:
$('.markComplete').submit( function( event ) {
event.preventDefault();
event.stopImmediatePropagation();
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // serialize the form
type: "POST", // GET or POST from the form
url: "pages/ajax/repairlogMarks.php", // the file to call from the form
success: function(response) { // on success..
refreshAllTabs();
}
});
return false;
});
button: 按钮:
<form class="markComplete">
<input type="text" style="display:none;" class="form-control" name="uniqueid" value='<?=$row['uniqueID'];?>'>
<input type="text" style="display:none;" class="form-control" name="markcomp" value='1'>
<button type="submit" class="btn btn-success">
<i class="btn-fa fa-only fa fa-check"></i>
</button>
</form>
Basically, I made the button into a form which I knew how to create an ajax request for. 基本上,我把按钮变成了一个我知道如何创建ajax请求的表单。
-- Update to make it work for multiple buttons that do the same function for different unique ID's. - 更新使其适用于为不同的唯一ID执行相同功能的多个按钮。
Well for since you're sending the ajax call using "POST", it seems to me that if(isSet($_GET['complete'])) would evaluate to false. 好吧因为你使用“POST”发送ajax调用,在我看来if(isSet($ _ GET ['complete']))将评估为false。 Also if your button is generated dynamically using php then change your click handler to the following: 此外,如果您的按钮是使用php动态生成的,则将您的点击处理程序更改为以下内容
$('document').on('click', '#markComplete', function (){
// Your code here
})
If you have more than one "Mark Complete" button; 如果您有多个“标记完成”按钮; you need to use classes rather than ID to bind the event. 您需要使用类而不是ID来绑定事件。
<button id="test">
Test 1
</button>
<button id="test">
Test 2
</button>
<script>
$('#test').click(function (e) {
console.log('test', e.target);
});
</script>
In this example, only the first button works. 在此示例中,只有第一个按钮有效。 jQuery will only return the first element when you specify an ID. jQuery只会在您指定ID时返回第一个元素。
If you use classes to bind the event; 如果使用类来绑定事件; both buttons will work: 两个按钮都可以工作:
<button class="test">
Test 1
</button>
<button class="test">
Test 2
</button>
<script>
$('.test').click(function (e) {
console.log('test', e.target);
});
</script>
i think you have an error in your javascript at this line... 我认为你在这行的javascript中有错误...
var input = input = $(this).text()
try to replace by this.. 试着用这个替换..
var input = $(this).text();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.