我是PHP的新手。我试图修复PHP代码的语法错误。但似乎没有用。我在s [i] [4]处得到了语法错误。如果我改为" + s [i] [4] +"它没有抱怨语法错误,但它没有读取实际值。我想我在PHP代码中使用了错误的变量但尝试了很多方法却失败了。实际上,如果状态(s [i] [4])被取消,我会试图隐藏付费按钮。请帮忙。
`<script type="text/javascript">
$(document).ready(function() {
var table = $('#myTransactionitems').dataTable(); //Initialize the datatable
var user = $(this).attr('id');
if(user != '')
{
$.ajax({
url: 'transactions',
dataType: 'json',
success: function(s){
console.log(s);
table.fnClearTable();
for(var i = 0; i < s.length; i++) {
table.fnAddData([
s[i][0],
s[i][1],
s[i][2],
s[i][3],
s[i][4],
s[i][5],
"<form method='post' action = 'donationSplit'><input name = 'donationid' type='hidden'\
value='"+s[i][0]+"'></input><input type='submit' value = 'Paid' <?php if (s[i][4]=='Cancelled'){ ?> style='display:none' <?php } ?> class='btn btn-sm btn-success pull-left '>\
</input></form><form method='post' action = 'donationSplit'><input name = 'donationid' type='hidden' \
value='"+s[i][0]+"'></input><input type='submit' value = 'Cancel' class='btn btn-sm btn-danger pull-right'>\
</input></form>"
]);
} // End For
},
error: function(e){
console.log(e.responseText);
}
});
}
});
</script>`
`
<?php
include('sessionstart.php');
include('session.php');
require_once("dbcontroller.php");
$db_handle = new DBController();
$user_id=$_SESSION['login_user_id'];
$query = mysql_query("select * from mytransactions_list where userid = '$user_id'");
while ($fetch = mysql_fetch_array($query)) {
$output[] = array($fetch[0], $fetch[1], $fetch[2], $fetch[3], $fetch[4], $fetch[5], $fetch[6]);
}
echo json_encode($output);
?>
`
答案 0 :(得分:0)
您不能在PHP中使用Javascript变量,因为首先执行PHP代码。
试试这个
"<form method='post' action = 'donationSplit'>\
<input name = 'donationid' type='hidden' value='"+s[i][0]+"' />\
<input type='submit' value = 'Paid' " + (s[i][4]=='Cancelled') ? "style='display:none'" : "" + "class='btn btn-sm btn-success pull-left ' />\
</form>\
<form method='post' action = 'donationSplit'>\
<input name = 'donationid' type='hidden' value='"+s[i][0]+"' />\
<input type='submit' value = 'Cancel' class='btn btn-sm btn-danger pull-right' />\
</form>"
答案 1 :(得分:0)
以下代码应该可以正常工作。我添加了一个名为disp1
的JS变量。它被赋值为“display:none;”如果if
条件匹配,否则它将为空。
for(var i = 0; i < s.length; i++) {
var disp1 = '';
if ( s[i][4] == 'Cancelled' ) {
disp1 = 'display:none;'
}
table.fnAddData([
s[i][0],
s[i][1],
s[i][2],
s[i][3],
s[i][4],
s[i][5],
"<form method='post' action = 'donationSplit'><input name = 'donationid' type='hidden'\
value='"+s[i][0]+"'></input><input type='submit' value = 'Paid' style='" + disp1 +"' class='btn btn-sm btn-success pull-left '>\
</input></form><form method='post' action = 'donationSplit'><input name = 'donationid' type='hidden' \
value='"+s[i][0]+"'></input><input type='submit' value = 'Cancel' class='btn btn-sm btn-danger pull-right'>\
</input></form>"
]);
} // End For
},
error: function(e){
console.log(e.responseText);
}
});
}
});