【发布时间】:2017-06-18 09:06:21
【问题描述】:
我有一个包含两列“姓名”和“年龄”的数据表,在用 Ajax 填充数据表后,我创建了一个按钮(每行一个)。 这工作正常。但是在用户点击一个按钮后,该行的两个字段(姓名和年龄)应该被发送到一个 php,然后发出这样的警报“你好,我的名字是 [name],我是 [age] 岁”。 (我需要这里的 php)。我写了一个可以调用php的代码,但我不知道如何将每一行的参数发送到php。我该怎么做?
我的js:
<script type="text/javascript">
$(document).ready(function() {
var oTable = $('#jsontable').dataTable(
$('td:eq(2)', nRow).html("<input type='button' id='button' onclick='test();' value='Click to Release'></input>");
); ///This is created after ajax
$('#load').on('click',function(){
var user = $(this).attr('id');
if(user != '')
{
$.ajax({
url: 'response.php?method=fetchdata',
type: 'POST',
data: {id1: $('#id1').val()},
dataType: 'json',
success: function(s){
console.log(s);
oTable.fnClearTable();
for(var i = 0; i < s.length; i++) {
oTable.fnAddData([
s[i][0],
s[i][1]
]);
} // End For
},
error: function(e){
console.log(e.responseText);
}
});
}
});
});
function test() {
$.ajax( { type : 'POST',
data : { },
url : 'process_form.php', // <=== CALL THE PHP FUNCTION HERE.
success: function ( data ) {
alert( data ); // <=== VALUE RETURNED FROM FUNCTION.
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>
process_form.php
<?php
function bb()
{
echo "hellooooo"; //How to pass the name and the age field to php here from each row of the datatable?
}
bb();
?>
【问题讨论】:
-
您需要使用链接datatables.net/reference/api/cell().data()中的api数据表示例中的函数cell().data()
-
我如何将参数发送到 php?
-
通过 ajax!首先,您需要创建一个触发器以从表中获取值。然后使用 Json 通过 ajax 发送值。如果您对这是如何完成的有任何疑问,请给我一些时间,我将尝试创建一个示例来说明它是如何完成的。
-
我对此非常陌生(这是我的第一个 ajax 函数),所以我只需要更新函数 test() 对吗?
-
我怎么知道行是什么?
标签: javascript php ajax datatable