[英]How to call php script with ajax?
I'm trying to create multiple tables where I can move table rows between the tables and have ajax call a php script that updates the DB with the new values, ie the new parent to the row. 我正在尝试创建多个表,在这些表中我可以在表之间移动表行,并让ajax调用php脚本,该脚本以新值(即该行的新父项)更新DB。
It's only html and javascript now and I'm wondering how the ajax part should look like to call a php script that updates the DB? 现在只有html和javascript了,我想知道ajax部分应该如何调用更新数据库的php脚本? And also if I should make some changes to the javascript/html part? 另外,如果我应该对javascript / html部分进行一些更改?
HTML: HTML:
<table class="tables_ui" id="t_draggable1"><h4>Table 1</h4>
<tbody class="t_sortable">
<tr>
<th>Title</th>
<th>Status</th>
<th>Creation date</th>
</tr>
@foreach($tasks as $task)
<tr class="row1" data-id="{{ $task->id }}">
<td>{{ $task->title }}</td>
<td>{{ ($task->status == 1)? "Completed" : "Not Completed" }}</td>
<td>{{ date('d-m-Y h:m:s',strtotime($task->created_at)) }}</td>
</tr>
@endforeach
</tbody>
</table>
<table class="tables_ui" id="t_draggable2"><h4>Table 2</h4>
<tbody class="t_sortable">
<tr>
<th>Title</th>
<th>Status</th>
<th>Creation date</th>
</tr>
<!-- More <td> rows here ... -->
</tbody>
</table>
<!-- More tables here ... -->
Javascript: Javascript:
<script>
$(document).ready(function() {
var $tabs = $('#t_draggable2')
$("tbody.t_sortable").sortable({
connectWith: ".t_sortable",
items: "> tr:not(:first)",
appendTo: $tabs,
helper:"clone",
zIndex: 999990
}).disableSelection();
var $tab_items = $(".nav-tabs > li", $tabs).droppable({
accept: ".t_sortable tr",
hoverClass: "ui-state-hover",
drop: function( event, ui ) { return false; }
});
});
</script>
For an $.ajax
call in js
you should pay attention to a couple of things regarding what you'd like to pass to the PHP
file and what you'd want as a return (response)
. 对于js
的$.ajax
调用,您应该注意一些有关您想要传递给PHP
文件以及作为回报(response)
。 A simple $.ajax
call for posting data down below: 一个简单的$.ajax
调用,用于在下面向下发布数据:
var Example1 = "123";
var Example2 = "456";
$.ajax({
type: "POST",
data: {
Example1: example1,
Example2: example2
},
url: "config/post.php",
success: function(){
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 100);
}
})
In the example above the variables (var)
, "Example1" and "Example2" will be inserted inside the request as the data. 在上面的示例中, variables (var)
,“ Example1”和“ Example2”将作为数据插入请求中。
Within your url:
you'll specify the post url to your php file. 在您的url:
您将指定发布网址到您的php文件。 In the PHP
file you're then able to obtain the data by using the second data {
Attributes in a $_REQUEST
see the example below: 然后,在PHP
文件中,您可以使用第二个data {
在$_REQUEST
属性,请参见下面的示例:
$example1 = $_REQUEST['example1']; $ example1 = $ _REQUEST ['example1']; $example2 = $_REQUEST['example2']; $ example2 = $ _REQUEST ['example2'];
echo $example1 . 回声$ example1。 $example2; $ example2;
To check if your data is obtained you could use the console.log()
function above your $.ajax
call to be sure. 要检查是否获取了数据,可以使用$.ajax
调用上方的console.log()
函数来确保。
var Example1 = "123";
var Example2 = "456";
// Check for the values
console.log(Example1 + Example2)
$.ajax({
type: "POST",
data: {
Example1: example1,
Example2: example2
},
url: "config/post.php",
success: function(){
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 100);
}
})
I hope this helps and if you have any questions please ask them in the comments section! 希望对您有所帮助,如果您有任何疑问,请在评论部分中提问! (: (:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.