JoshEir Posted December 20, 2020 Share Posted December 20, 2020 Hello, everyone. I have code that effectively calls a PHP page with AJAX (no JQuery). The PHP responds fine with an echo. Here is the javaScript that calls the PHP so refreshing the page doesn't need to be done. function deleteCategory() { var e= document.getElementById("dropDown1"); var var1 = e.options[e.selectedIndex].text; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = alerta() { if (this.readyState == 4 && this.status == 200) { document.getElementById("insert").innerHTML = xmlhttp.responseText; } }; xmlhttp.open("GET", "deleteRow.php?q=" + var1 , true); xmlhttp.send(); } How do I call a PHP function on the same page while preserving the above AJAX. Sincerely, Joshua Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/ Share on other sites More sharing options...
gw1500se Posted December 20, 2020 Share Posted December 20, 2020 Not sure what you are asking. This is all done on the client side so JavaScript can control what is displayed. Calling a PHP function with Ajax does not change anything on that page unless the JavaScript makes the change. Keep in mind that PHP is server side only and is stateless. JavaScript is client side and can control the current page completely. Thus a different JavaScript/Ajax function will not effect this function. Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583290 Share on other sites More sharing options...
Barand Posted December 20, 2020 Share Posted December 20, 2020 There's an example here of processing an ajax request in the same page Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583291 Share on other sites More sharing options...
JoshEir Posted December 20, 2020 Author Share Posted December 20, 2020 (edited) You might not be able to put php in javascript script, but you certainly can put them on the same page. I'm wondering how to call a php on the same page with a AJAX and not JQuery. I saw this earlier and lost the link Edited December 20, 2020 by JoshEir more text Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583300 Share on other sites More sharing options...
maxxd Posted December 20, 2020 Share Posted December 20, 2020 (edited) You don't need jQuery to run AJAX, you just need JavaScript. So, either go modern with fetch or use XMLHttpRequest (or ActiveXObject). If you want to make multiple AJAX calls from the same page, nothing's stopping you - pick a trigger and call a different PHP target for the request. If you want to get tricky, pass an 'action' parameter to your PHP and decide the action to run based on that - let resp = fetch(url, { method: 'get', body: JSON.stringify({ 'action': 'doSomething', 'data': ['data','more','etc'] }) }); function doSomething(){ switch($_GET['action']){ case 'doSomething': doSomething($_GET['data']); break; case 'doSomethingElse': doSomethingElse($_GET['data']); break; } } (not tested code, just a basic idea) Edited December 20, 2020 by maxxd 1 Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583301 Share on other sites More sharing options...
JoshEir Posted December 21, 2020 Author Share Posted December 21, 2020 (edited) Hmm... well I found this about javascript and php together: <?php // Call PHP function from javascript with parameters function myphpfunction($x, $y){ $z=$x+$y; return 'The sum is: '.$z; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <script type="text/javascript"> alert("<?php echo myphpfunction(4,90)?>"); </script> </head> <body> </body> </html> Now it would seem that this does not refresh the page, I am wondering if it is as good as AJAX? And maxxd, It looks like the fetch uses a URL, am I correct? Josh I guess this would be for a return only. Edited December 21, 2020 by JoshEir Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583303 Share on other sites More sharing options...
JoshEir Posted December 21, 2020 Author Share Posted December 21, 2020 (edited) I guess this would work: function onButtonPress() { <p> <?php echo $aVariable ?> </p> } I don't know what I was thinking, it is no replacement for AJAX. Edited December 21, 2020 by JoshEir Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583305 Share on other sites More sharing options...
maxxd Posted December 21, 2020 Share Posted December 21, 2020 There's no AJAX in anything you just posted. I'm also assuming that's an example because there's nothing there that JavaScript alone couldn't handle on its own. The PHP will parse the call to myphpfunction() on the server before the page gets sent to the browser - the JavaScript will be this: <script type="text/javascript"> alert("The sum is: 360"); </script> If that's all you need, that's fine. If you need to call myphpfunction() with different values based on a user selection and you don't want to reload the entire page, you're going to have to use JavaScript and AJAX. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583306 Share on other sites More sharing options...
JoshEir Posted December 21, 2020 Author Share Posted December 21, 2020 (edited) This would be neat: document.getElementById("insert").innerHTML = <?php $descriptionVar; ?>; Edited December 21, 2020 by JoshEir more text Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583307 Share on other sites More sharing options...
maxxd Posted December 21, 2020 Share Posted December 21, 2020 The bigger question at this point is what is the actual problem you're trying to solve? Given what you've just posted, you'd be far better off simply injecting $descriptionVar into the markup on the server. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583309 Share on other sites More sharing options...
NotionCommotion Posted December 22, 2020 Share Posted December 22, 2020 On 12/20/2020 at 5:02 PM, JoshEir said: This would be neat: document.getElementById("insert").innerHTML = <?php $descriptionVar; ?>; Hello JoshEir, You will want to spend a few minutes or likely much more understanding the difference between your server code (i.e. PHP) and your client code (i.e. JavaScript or maybe just JavaScript disguised as jQuery. Assume only text can be shared between them so don't expect too much (JSON/etc allows us to push the text barrier and get more) and pay attention to where the content exists. No matter how high-tech the world seems sometimes, it really isn't. Quote Link to comment https://forums.phpfreaks.com/topic/311900-with-ajax-opening-a-php-function-on-the-same-page-without-jquery/#findComment-1583345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.