Jump to content

Simple call of php function using ajax


penisland
Go to solution Solved by JIXO,

Recommended Posts

I would like to know how to call a php function using javascript. I've done some googling and found that I need to use ajax. I read some tutorials and don't understand how to use ajax.

 

All I want to do is execute a php function, which writes to a txt file, from the execution of a javascript function. I don't care if the webpage refreshes or not; I'll just make the javascript function refresh the page.

 

Something like this:

<?php include 'banusers.php'; ?>
<script>
function banUser()
{
<?php writeIPToFile(); ?>
alert("You have been banned!");
window.location.reload();
}
</script>

<p>Click <a href="javascript:banUser();">HERE</a> to ban yourself!</p>

Would someone be kind enought to write an example for me?

 

Edit: fixed typo

Edited by penisland
Link to comment
Share on other sites

  • Solution

Hello penisland,

 

Just to be clear, I don't know if its possible to call a function by its name form ajax script, but you can call a .php page, consider index.html :

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
var a = new XMLHttpRequest();
a.open("GET","test.php");
a.onreadystatechange = function() {
if( a.readyState == 4) {
if( a.status == 200) {
alert("Worked");
}
else alert("HTTP error "+a.status+" "+a.statusText);
}
}
a.send();
</script>
<title>Ajax call to PHP</title>
</head>
<body>
</body>
</html>

AJAX will send a GET reguest to test.php which is placed in the same directory as index.html, now consider this code for test.php

<?php

// Defining the function banUser()

function banUser() {
    // Code .....
}

// Executing banUser()

banUser();

?>

This way test.php takes care of executing the banUser() method.

 

You can also pass get parameters to test.php, in the AJAX script

a.open("GET","test.php?para1=val1&para2=val2");

Then you can handle these values from PHP's $_GET super global.

 

I'm goning to make a personal note about these lines of codes I've wrote, they're just poor examples with alot of security flaws,

try to learn about AJAX before adding pieces of code that might damage your site.

 

 

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.