Reputation: 913
I am creating a CMS that uses an MVC-structure to create/modify/delete pages of a website.
I have lots of objects where I store my data in.
E.g. a $sitemodel
with:
$site-id
$page-template
To create a new page, I added a button in the HTML:
<ul class="nav nav-pills nav-stacked nav-bracket">
<li class="_add-page">
<a href="#user/sites/new">
<i class="fa fa-plus"></i>
<span> Voeg een pagina toe</span>
</a>
</li>
</ul>
I use jQuery to create an event:
$(document).ready(function(){
$("#test-id").click(function(ev){
ev.preventDefault();
$(".site_creator").load("add_page.php");
});
});
The add_page.php
calls the following function:
$pagecontroller->new_page($sitemodel->ID);
This on its turn calls the following function:
public function new_page($site-ID)
{
$pagemodel = new page_model;
$page_ID=$pagemodel->insert_pagemodel($site-ID);
return $page_ID;
}
And finally the SQL function from the pagemodel:
public function insert_pagemodel($site-ID)
{
$sSQL = "INSERT INTO `pages` (object) VALUES (".$site-ID.");
if (mysql_query($sSQL)) {$this->ID = mysql_insert_id();} else {return false;}
return $this->ID;
}
What I am trying to understand is: how do I get to use the methods from my $page-controller
and $site-controller
in add_page.php
?
From my research I find two options:
add_page.php
",{})I am trying to use option 2, but I have no idea how to get my data from PHP into the JavaScript? And is it even possible to add the methods of the classes into the ajax call?
I am trying to see the whole picture here, but it looks to me like I am missing something?
How do I get the data and methods from my objects into the create_page.php
?
Upvotes: 1
Views: 156
Reputation: 399
@Abayob I'm not quite sure of what you want to do, but:
How do I get the data and methods from my objects into the create_page.php?
You can pass data, but you can't pass methods to php and vice versa. The best way to pass data from php to js is by using json: php -> http://php.net/manual/en/function.json-encode.php
check here for a example on how it works: https://jonsuh.com/blog/jquery-ajax-call-to-php-script-with-json-return/
Upvotes: 0
Reputation: 1858
You need to create an AJAX call from your page which targets the PHP file on your server : -
$(document).ready(function(){
$("#test-id").click(function(ev){
ev.preventDefault();
$.ajax({
method: "POST",
url: "add_page.php/new_page",
data: { // any data you wish to pass to the function }
})
.done(function( data ) {
// You code for when the function has completed
});
});
});
This should get you started- however, you need to work out how you will send the variable you need to use in the PHP & SQL methods ie. $sitemodel->ID
.
Upvotes: 1