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>
Advertisement
Answer
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']); } } ?>