Reputation: 55
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>";
}
?>
Upvotes: 0
Views: 74
Reputation: 952
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.
Upvotes: 1
Reputation: 74738
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);
}
Upvotes: 1
Reputation: 26288
//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.
Upvotes: -1