Reputation: 363
How to call ajax without an external file. for example if i want to delete some row i am calling an external file named delete.php to perform delete operation . i want to keep that delete.php code in main file only. suppose i have one page where several messages are there i want to keep that delete code in that page called index.php only. please guide how to do that . suppose i want to load auto scroll webpage then i dont want to call an exteral file as i have to define everything in that external file once again . so guys please tell how to call ajax url in parent file only .
Upvotes: 0
Views: 1590
Reputation: 1447
If you want to define the delete logic in the main html file you can pass a query parameter to the .ajax function then test for that parameter on the php side.
$.ajax(url,
{
data: "action=delete"
success: function(){...}
})
Then on the php side
<?php
if($_GET['action'] == 'delete'){
logicToDelete(x);
} else if {
logicToDisplayPage();
}
?>
You mentioned that you wanted to do this because you didn't want to have to re-define everything in everything again in the delete.php file. If your application has a lot of variables that every page needs, you probably want to put all of those shared things into a single file then include that file in every other file.
common.php:
<?php
$setting1 = "foo";
$setting2 = "bar";
$setting3 = "baz";
?>
index.php
<?php
include_once('./common.php');
echo $setting1;
?>
Upvotes: 1