Php and jQuery $ .ajax ()

The website I am developing is structured like this:

It has a function that switches the module for the contents of the home page when installed $_GET['module'], for example:

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

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

For example, I wrote getNews()to work 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 do not use a unique file for each module; all module operations are part of a unique function in which the action switches again to change the result.

If I want to receive news from the database, I have to use the following URL: ?index.php&module=getnews&id=1

:
jQuery $.ajax() (), , get? ?

+3
3

URL- $.ajax(). , axjax, - , , .

php ajax,

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

    // dont output layout
}
+1

"test.php", :

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

js GET test.php . script , .

+1

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>
0
source

All Articles