1

My first contact with Ajax is happening right now, and I'm kind a confused. I've read many of questions asked, but I'm not able to read the answer, that is most likely here somewhere.

Situation is, I'm using OOP PHP approach, and all I do go through index.php with parameters. So I do not call any other .php file in form posts, button clicks..

I've created an HTML listbox (which I'd like to remove vertical scrollbar, but that's just a bonus to resolve), which feeds my categories in it.

Now, by clicking each category I'd like to call certain function that would then generate output for the other div.

function swapContent(){
$("#myPresentDiv").html('<img src="../../imgs/ajax-loader-big.gif"/>').show();
var cat = $('#listbox').val();
$("#action").change(alert(cat));
var url = "&s=".cat;
$.post(url, {contentVar: cat} ,function(data) {
   $("#myPresentDiv").html(data).show();
});  

}

So, my JQuery script picks up correct Category, I alert it to alert dialog, so I'm sure that's fine, and then with code as it is at the moment, I reload my whole page so I get, page in page in page in page...

I'm trying to figure out how to write JQ ajax call, that would return only the results, not the whole page.

can I put URL "index.php&s="cat, and then somehow tell to ajax "go through index, call function displayresults ($cat); ?

Hope everything I wrote make sense to you :)

Tnx.

6 Answers 6

1

The url's your ajax function call, must return only the page parts and not the whole html document. If you have

$.post('ajax.php',data,function(d){
  $('#responsediv').html(d).show();
});

The file ajax.php must only return the page parts,like

<div>This is the new content</div>    

so you will not have page inside page.

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

3 Comments

I've seen this in some answers already, but I do not understand this. First off, I do not have ajax.php. I can create it, but then in it I need to re-create session, database object, menu object, article object, and copy 5 functions that I use in my existing classes to generate something, that I actually get only with calling direct link index.php?p=112&s=5. So in general,my JQuery only reads the S (cat) number, and should reach articles from database in category 5.
You cannot call ajax to same index.php as that would return you the whole html document. You need different control page for the ajax requests. You can reuse your sessions from index.php in these pages as well.
BTW you can make ajax calls to index.php as long as you process those requests and output before the actual content of index.php starts and exit. This way you wont have pages inside page. But all the codes will be in the same page making it hard to debug
0

If you look at the frameworks or cms out there, they basically have routes that map calls to your index.php function to methods of the controller.

This is a complex argument, you could try to start out reading this article

Comments

0

Yeah, that makes sense. Your question is basically: when you get a result of an AJAX op and insert it into your page, it inserts the whole layout again, rather than the template.

OK, so the solution is to make a call to a PHP script that is "unstyled" i.e. has no template data. Your PHP script should therefore just output a short HTML snippet rather than a page (you might have a 'header' and 'footer' that can be removed for this page). What action you need to take depends what you're using on the server side - framework? CMS? Custom PHP app?

2 Comments

It's my home made CMS, and I'm quite new in OOP in general (only 3 months so far), so I still do not understand what I'm doing completely ;) It is actually back-end of CMS / order shop, where I'm handling some articles (trying to)....
Coolio. Well... if it's your own code, you'll know how to stop the layout/header/footer html appearing :)
0

I did the exact thing for a internal app quite some time ago....What happened was i was passing the class name, function name and the function parameters via ajax variables and reading the same in php at the backend and then call the appropriate function in the class with those paraeters.

The PHP code:

$option = trim($_GET['v']);

switch ( $option ) {
    case 1:
        $sid    = trim($_GET['q']);
        $page   = trim($_GET['p']);
        $method = trim($_GET['m']);

        $class  = new $page( $link );
        echo $class->$method( $sid );
    break;
    case 2:
        $page   = trim($_GET['p']);
        $method = trim($_GET['m']);

        $class  = new $page( $link );
        echo $class->$method();
    break;
    default:
        echo '';
    break;
}

But this was an internal app, so there was no injection attacks, xss,xsrf,session hijack issues....things might differ for you

Hope this helps.

Comments

0

I think you are searching for a GENERAL strategy to handle ajax requests its upto you

for example Server Side Ajax

unless you are using a specific framework (CI , yii etc)

Comments

0

You might want to look into some frameworks, as they can make this for you infinitely easier to implement:

http://demo.atk4.com/demo.html?t=20

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.