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>";

    }
?>
4
  • You are getting any error in firebug? Commented Jul 20, 2016 at 8:21
  • Things that you have to note is that. you have to include config.php in dbconnect.php. and include dbconnect.php in your index.php. Commented Jul 20, 2016 at 8:22
  • the javascript in delete.php will not be executed as you are intending Commented Jul 20, 2016 at 8:22
  • You can't really inject a script and have it run on the client side just like that. You need to evaluate the script on the client side in order for the alert('jjjj') to pop up. Commented Jul 20, 2016 at 8:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This answers the question properly.
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.

7 Comments

How to alert the script of delete.php? you have not answered it.
It's updated, there's only a change in what you alert on success.
No! Still not answered it.
check this answer and the demo. stackoverflow.com/a/38476157/1059101
@trajchevska I don't think you are answering the question
|
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.