Jump to content

With Ajax, opening a PHP function on the same page without JQuery.


JoshEir

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by JoshEir
more text
Link to comment
Share on other sites

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 by maxxd
  • Like 1
Link to comment
Share on other sites

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 by JoshEir
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.