3

The website I'm developing is structured in this way:

It has a function that switches the module for the homepage content when $_GET['module'] is set, example:

function switchmodules()
{
    $module=$_GET['module'];
    switch($module)
    {
        case 'getnews': news(); break;
        default: def();
    }
}

As you can see, this switch calls another function for each module.

For example I wrote getNews() to work in this way:

function getNews()
{
   $id=$_GET['id'];
   if(!id)
   {
       //CODE TO LIST ALL NEWS
   }
   if(isset($id))
   {
       //CODE TO GET ONLY 1 NEWS BY ID
   }

}

So, as you can see I'm not using an unique file for each module; all operations of a module are part of an unique function in which an action is switched again to change the result.

If I want to get a news from database I should use an url like this: ?index.php&module=getnews&id=1

My question now is:
With jQuery and $.ajax() method is there a way to get a news (for example) using this structure based on functions switched by a get? Or do I have to change everything and make a file for each function?

5
  • you do not have to change anythink it only how you display the result if you just send result in a form that renders to html than you should be fine Commented Apr 14, 2012 at 20:01
  • 1
    Your code is ok, now, read about jQuery $.ajax (api.jquery.com/jQuery.ajax) Commented Apr 14, 2012 at 20:02
  • i can't still understand, in $.ajax function url:"" i cant pass an url like ?index.php&module=getnews&id=1 so, can you please make me a simple example to do that? Commented Apr 14, 2012 at 20:05
  • You should understand that PHP runs on the Server not the client. It will perform your scripts and then return a pure HTML page. Any functions written in Javascript or its libraries will be executed on the client, after the server has loaded the pure HTML page (php processing is done). Commented Apr 14, 2012 at 20:08
  • Yes, you can, see my answer below. And, to understand, learn about jQuery serialize (api.jquery.com/serialize) Commented Apr 14, 2012 at 20:16

3 Answers 3

1

you can simply call the same url via $.ajax(). the only thing which should be changed for axjax calls is that you don't output your layout, but only the news itsself.

in php you can check for an ajax request like

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

    // dont output layout
}
Sign up to request clarification or add additional context in comments.

Comments

1

If this code is in say 'test.php' you can do:

<script>
    $.get(
        '/test.php',
        {
            module: 'news',
            id: 1
        },
        function( data ) {
            alert( data );
        }
    );
</script>

And js will send GET request to test.php with needed params. Then your server script will decide how to process request.

Comments

0

jQuery

$(document).ready( function() {
    var form = '#my_awesome_form';

    $(form + ' input[type=submit]').click(function(e) {
        e.preventDefault();

        $.ajax({
            type: "GET",
            url: 'index.php',
            data: $(form).serialize(),
            success: function( response ) {
                alert('DONE!');
            }
        });
    });
});

HTML

<form id="my_awesome_form" // OTHERS PROPERTIES //>
    // LOT OF FIELDS HERE //
    <input type="submit" value="Send">
</form>

1 Comment

i would add a return false to the click, otherwise the brwoser will submit the form and cancel the ajax request

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.