0

I don't know much about the ajax and its functionality. I wanted to call a php page from another php page through ajax call but its not working. I just wanted the alert to be displayed which is in the "delete.php" when I press a delete button from the "index.php".

dbconnect.php

<?php
$conn = new mysqli($host, $user, $password, $database);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
?>

config.php

<?php
$host="192.168.20.171";
$user="production";
$password="******";
$database="*****";
?>

index.php

<html>
<?php
include 'config.php';
include 'dbconnect.php';
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <table border="1">
        <tr>
            <th>categoryId</th>
            <th>category</th>
            <th>Operations</th>
        </tr>
        <?php
        $sql_query = "select category_id,category from cscart_category_descriptions;";
        $result = $conn->query($sql_query);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                echo '<tr><td>'.$row["category_id"].'</td><td>'.$row["category"].'</td><td><button class="deletedata" data-id="'.$row['category_id'].'">Del</button></td></tr>';
            }
        }
        else {
            echo "0 results";
        }
        ?>
        <script>
        $(document).on('click','.deletedata',function(){
            id = $(this).attr('data-id'); // Get the clicked id for deletion 
            $.ajax({
                type:'GET',
                url:'delete.php',
                data:{delete_id:id},
                success:function(response){
                    if (response == 'ok') {
                            alert("succes");
                    } else {
                        alert("fail");
                    }
                }
            })});
        </script>
    </table>
</html>

delete.php

<?php
include 'config.php';
include 'dbconnect.php';
    if($_GET('delete_id'))
    {
        echo"<script>alert('jjjj');</script>";

    }
?>
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
65th bit
  • 55
  • 1
  • 11

3 Answers3

1

What you echo in the delete.php file is what is returned as response of the ajax.

So in this code:

$.ajax({
    type:'GET',
    url:'delete.php',
    data:{delete_id:id},
    success:function(response){
        if (response == 'ok') {
                alert("succes");
        } else {
            alert("fail");
        }
    }
})});

The response is <script>alert('jjjj');</script>, which makes your code enter the else and alert fail.

What you need to do is echo ok in your delete.php and then alert jjjj on success. That would be:

//delete.php
include 'config.php';
include 'dbconnect.php';
if($_GET('delete_id'))
{
    echo "ok";
}

Your ajax call would be similar, with the only difference of what you alert on success.

$.ajax({
    type:'GET',
    url:'delete.php',
    data:{delete_id:id},
    success:function(response){
        if (response == 'ok') {
            alert("jjjj");
        } else {
            alert("fail");
        }
    }
})});

Usually, when you do ajax calls and get a response, your next actions would depend on the received response, so most often you'll have something like alert(response) (or any other action that utilizes what's been received from the server), instead of doing a static alert.

You should consider learning a bit more about AJAX before starting using it.

trajchevska
  • 942
  • 7
  • 13
  • How to alert the script of `delete.php`? you have not answered it. – Jai Jul 20 '16 at 08:26
  • It's updated, there's only a change in what you alert on success. – trajchevska Jul 20 '16 at 08:30
  • No! Still not answered it. – Jai Jul 20 '16 at 08:35
  • 1
    check this answer and the demo. http://stackoverflow.com/a/38476157/1059101 – Jai Jul 20 '16 at 08:35
  • @trajchevska I don't think you are answering the question – Ivan Jul 20 '16 at 08:49
  • Returning script from server side and appending it to the html is not a good practice. That is why my answer implies that the delete.php script should return the 'ok' message and all the rest should be handled in ajax success. It completely answers the question and includes notes that could benefit both the OP and other users. – trajchevska Jul 20 '16 at 08:57
  • I just wanted the ajax call to be made. Please alerts are only to verify that i made the ajax call to the delete.php. So, please answer point to point. – 65th bit Jul 20 '16 at 08:57
1

As you have a GET request to delete.php, you can have that script from that php to your current script but you have to append it:

success:function(response){
    // here response is the script tag from delete.php
    $(document.head).append(response);
}

A plnkr DEMO in action.

Jai
  • 74,255
  • 12
  • 74
  • 103
-1

//delete.php

<?php
include 'config.php';
include 'dbconnect.php';

echo 'ok';

?>

whatever you echo in php page will get back to you in ajax response.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59