1

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
  • etc.

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:

  1. $_SESSION variable (rather not, because then I have to add session to all my files)
  2. Adding them into the .load function, after ("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?

3
  • 1
    For a start, I would try to use .get instead of .load, but it's ok: using .load you can pass whatever parameters you want by adding them to the script name (add_page.php?param1=foo&param2=bar... and calling the appropiate functions inside the script upon the receiver parameters you can recover from global variable $_GET Commented Sep 22, 2015 at 9:58
  • 1
    Create a route that targets that methods and use AJAX to manipulate with it. Commented Sep 22, 2015 at 10:00
  • How to create a route? Commented Sep 22, 2015 at 10:33

2 Answers 2

1

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.

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

6 Comments

Thanks :D In the url you added "/new_page". What happens when you add that? Does it point to some function?
@Abayob - yes it points to your new_page function that you defined in your question
I am trying to call a function in the phpfile using your "/new_page"-example, but it does not seem to work. Do you have any documentation on this?
@Abayob - which function are you trying to call? Is the code for the function provided in your initial question?
No, I am trying to call a function "new_page" in the add_page.php that is called by your example. In this "new_page" function I then create a object that contains the "new_page" function mentioned in my initial question.
|
0

@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/

5 Comments

So if I understand right, NOONE uses methods in an PHP-file called by AJAX to write something to the database?
@Abayob you are confusing all the concepts. Js/ajax -> front end / client side. php -> server side. You can't call a php method from the front end nor you can pass methods from the client to the server side. What you do is, you call the server side and instruct it to run a method on is side. :) It is completely different :) I'm guessing that you are not understanding how this client to server side (or front to back end) works.. right?
Well, I guess my boss is putting me on the wrong track. He added most of the methods in the objects. Why on earth would I be use a add-page method from an object other than after a event on the client-side? Do you might have some information to read on the interaction between the two?
@Abayob quick note: check this out to understand the difference between a method and a function stackoverflow.com/questions/4841605/…
@Abayob you boss is not leading in the wrong direction. What you need to understand is how the client side and the server side communicate and how you can send requests from the client to the server side. To know what acrion you need to do on the server side you can send the information on the $_POST or on the $_GET. google on how to do it. (this was what Amarnasan was talking about)..

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.