kagami
kagami

Reputation: 44

call function url page with ajax

I have tried several times but it didn't work. I want to call the function nama() on the dom_area.php page.

This is dom_area as target

<?php
    function nama()
    {
        echo $_POST['data_area'];
    }
?>

and this is the page where I call the function

<button id='acc'>asas</button>
<script>
    $(document).ready(function() {

        $('#acc').click(function() {

            let data_area = 2;
            let url_address = "<?= 'dom_area.php/nama()'; ?>";

            $.ajax({

                url:url_address,
                type:'post',
                data:{data_area:data_area} ,

            }).done(function(output){

                console.log(output);
                // alert(data);
            });
            // console.log(url);
        });

    });
</script>

Upvotes: 0

Views: 52

Answers (3)

RiggsFolly
RiggsFolly

Reputation: 94642

You can only cause a php file to be launched, NOT a specific function within that script

However if you pass a parameter to control what to do in the script you can achieve what you are trying to do

<button id='acc'>asas</button>

<script>
$(document).ready(function(){
    $('#acc').click(function(){
        let data_area = 2;
        $.ajax({
            url:'dom_area.php',     // the script file name
            type:'post',
            // add a control parameter 
            data:{control:"Nama", data_area:data_area},
        }).done(function(output){

            console.log(output);
            // alert(data);
        });
        // console.log(url);
    });
});
</script>

Now in the PHP

<?php
function nama()
{
    echo $_POST['data_area'];
}


if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    // form was submitted

    switch($_POST['control'])
        case 'Nama':
            nama();
            break;
        default:
            echo json_encode(['status'=>'failed']);
    }
}
?>

Upvotes: 1

Happy Son
Happy Son

Reputation: 91

Please try this in call part.

<button id='acc'>asas</button>
        <script>
            $(document).ready(function(){

                $('#acc').click(function(){

                    let data_area = 2;

                    $.ajax({

                        url:'dom_area.php/nama',
                        type:'post',
                        data:{data_area:data_area} ,

                    }).done(function(output){

                        console.log(output);
                        // alert(data);
                    });
                // console.log(url);
                });

            });

        </script>

Upvotes: 0

Karolis
Karolis

Reputation: 2618

try this:

<?php include_once('dom_area.php') ?>

<button id='acc'>asas</button>

    <script>
        $(document).ready(function(){

            $('#acc').click(function(){

                let data_area = 2;
                let url_address = "<?= json_encode(nama()); ?>";

                $.ajax({

                    url:url_address,
                    type:'post',
                    data:{data_area:data_area} ,

                }).done(function(output){

                    console.log(output);
                    // alert(data);
                });
            // console.log(url);
            });

        });

    </script>

Upvotes: 0

Related Questions