Azur Pazur
Azur Pazur

Reputation: 125

Sending data ajax to php, not refreshing whole page, but execute php again in the same file

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:

Thank you.

Upvotes: 0

Views: 126

Answers (2)

Double Prince
Double Prince

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

TheValyreanGroup
TheValyreanGroup

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

Related Questions