Execute a PHP function in a file of functions from a JavaScript function -
i want call php function javascript function.
"why?" might ask?
i have series of php functions organized in separate file custom_functions.php
i learning how build javascript , jquery how bridging gap. more familiar php, finding way through window.
i have list of elements constructed in index.php. list of items constructed on fly database , id attribute created based on id of record:
echo "<span id="content_" . $row{'unique_id'} . "\" onclick=\"displaydetails(" . $row{'unique_id'} . ")\">";
displaydetails(record_no)
javascript function in external javascript file.
for tried this:
function displaydetails(record){ data = {}; //data = <?php retrievecontent(content_id); ?> $('.output').html("testing function - section " + section_number); }
but of course not work because php server side action , javascript client-side action.
my particular problem enough different this one leave me stumped.
i have php file , within want ajax call specific function range of functions in file.
what's not clear how call function in file ajax. pass in url this: "custom_functions.php?functionname=displaydetails&record=<some number>
" ?
for now, not want use jquery ajax, i'd understand fundamentals of xmlhttprequest purely pure javascript/dom perspective.
yes, have call php file mentioned.
when calling php script ajax, return data json formation. echo in php become ajax result.
and javascript job creating elements show , inject data.
however, can inject result html of php script directly dom.
example1: injecting ajax response data directly dom
javascript
axios.get('/example_api.php').then(resp => { // in case, gonna put response data innerhtml wanna show. somedomeelement.innerhtml = resp.data; });
php (example_api.php)
echo "<p>hello world</p>";
https://developer.mozilla.org/en-us/docs/web/api/xmlhttprequest
example2: when ajax response data json
javascript
axios.get('/example_api.php').then(resp => { resp.data.foreach(function(item) { // in case, have create dom elements show data createelement(item); }); });
php(example_api.php)
echo json_encode(array('data1', 'data2'));
update
i update answer here if comment more questions.
Comments
Post a Comment