Reputation: 125
I have made index.php page with buttons that generate tables. PHP code/script bellow is generated after clicking these buttons.
All parts of code that you can see here are in admin.php file but visible in index.php.
I have generated table with php with mysql data. I also generated IDs for buttons that should be used for deleting a table row without refreshing the whole view.
I have JQuery function with JQuery + Ajax that is triggered after pressing one of the buttons for deletion.
$(".delete-but").click(function() {
if(confirm("Are you sure you want to delete this table row?")){
var butId = $(this).attr('id');
$.ajax({
type: "post",
data: {
action: 'expo',
deletion: 'butId'
},
success: function(result) {
content.html(result);
}});
}
else{
return false;
}
});
Code for generating part of the table with buttons:
$data .="<td>"."<button id=".$row[id_expozice]."class='delete-but'><form action='admin_script.php.php' method='post'>Delete</button>"."</td>";
I have general button argument in top of JQuery function. If I press a button for deletion, confirm panel pop up, then I press OK and I am able to send an alert to page if I want. I am passing an ID value for clicked button for deletion, that I wanna get back to php used as a sign to delete row in a table, to JQuery function (butId). I want to use Ajax to send butId back to php and run php script again to delete row in Mysql.
I echoed:
echo "I want to be here"; Where I want to be.
//echo $_SESSION["user"];
if(isset($_POST['action'])){
$data = '';
//expo
if($_POST['action'] == "expo") {
$sql = "SELECT id_expozice, jmeno, id_zamestnance, typ_expozice, trvani_od, trvani_do, id_pronajimatel FROM Expozice";
$result = mysql_query($sql)or die(mysql_error());
echo "I am here";
while ($row = mysql_fetch_assoc($result)) {
if(isset ($_POST['deletion'])){
if ($_POST['deletion'] == $row.[id_expozice]){
echo " I want to be here "; //to delete row table and mysql.
}
}
else{
$data .= "<tr>";
$data .="<td>".$row[id_expozice]."</td>";
$data .="<td>".$row[jmeno]."</td>";
$data .="<td>".$row[typ_expozice]."</td>";
$data .="<td>".$row[trvani_od]."</td>";
$data .="<td>".$row[trvani_do]."</td>";
$data .="<td>".$row[id_pronajimatel]."</td>";
$data .="<td>".$row[id_zamestnance]."</td>";
$data .="<td>"."<button id=".$row[id_expozice]." class='delete-but'><form action='admin_script.php.php' method='post';'>Delete</button>"."</td>";
$data .="</tr>";
}
}
$final_data = '<table><thead><tr><td> Exposition ID</td><td>Name</td><td>Type</td>
<td>Duration from</td><td>Duration to</td><td>Lessor ID</td><td>Employee ID</td><td>Delete</td></tr></thead><tbody>'.$data.'</tbody></table>';
echo $final_data;
}
How could I set Ajax to send butId back to php and run php script again?
Thank you.
Upvotes: 0
Views: 126
Reputation: 13
I think you should try adding dataType: 'json' to your ajax call lets see the result
$.ajax({
type: "post",
data: {
action: 'expo',
deletion: 'butId'
},
dataType:"json",
success: function(result) {
content.html(result);
}});
Upvotes: 0
Reputation: 3599
You have a .
where it should not be. Change this line
if ($_POST['deletion'] == $row.['id_expozice']){
To
if ($_POST['deletion'] == $row['id_expozice']){
You also don't have any of your row objects in quotes.
$.ajax({
type: "post",
URL: "admin.php",
data: {
action: 'expo',
deletion: 'butId'
},
success: function(result) {
content.html(result);
}});
Upvotes: 1