Santosh Reddy Santosh Reddy - 4 months ago 29
JSON Question

If condition array declaration error inside input tag for php and datatable code

I am new to PHP. I'm trying to fix the syntax error for PHP code. But seems not working. I'm getting the syntax error at s[i][4]. If I change to "+s[i][4]+" it is not complaining about syntax error but it is not reading the actual value. I think I'm using the wrong variable inside PHP code but tried lot ways but failed. Actually I'm trying to hide paid button if the status(s[i][4]) is cancelled. Please help.

Java script code



`<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>`


Trasaction.php code



`

<?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);
?>
`

Answer

The below code should work fine. I have added a JS variable named disp1. It's assigned the value of "display:none;" if the if condition is matched, otherwise it will be blank.

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);
} }); }
});

Comments