Home > Net >  Show div by calling php function with ajax
Show div by calling php function with ajax

Time:11-01

I have a link, which brings other information according to the combine value. When I click on this link, I want to show the values ​​by opening a div on the same page with ajax. Link :

<a  href="index.php?module=events&action=generated&combine=<?php echo $C->combine ?>"><?= $L['btnDelete']; ?></a>

waiting for solution please

CodePudding user response:

You could define a function like this to make the request and display the result:

function ajaxRequest(){
    fetch("index.php?module=events&action=generated&combine=<?php echo $C->combine ?>><?= $L['btnDelete'] ?>")
        .then(response => response.json())
        .then(body => console.log(body))
}

Then you could add an event listener to a button to trigger the request:

const button = document.querySelector('button');
button.addEventListener('click', function(){
    ajaxRequest();
}

In the ajaxRequest() function I have used the .json() method of the response object (response.json()). For that to work your PHP script at the URL provided as the argument to the fetch() function would need to echo a JSON string. You could achieve that with echo json_encode($myArray). The query parameters will be available to your PHP script as `$_GET['module'] etc.

CodePudding user response:

how do i show it inside a div?

  • Related