Call PHP function from url?
One quick way is to do things like
something.com/myscript.php?f=your_function_name
then in myscript.php
if(function_exists($_GET['f'])) {
$_GET['f']();
}
But please, for the love of all kittens, don't abuse this.
Call function and pass parameter over url PHP
It is most certainly wrong and insecure (imagine what would happen if you tried calling it with action=unlink¶m=somefile.php
), but you could do something like:
With URL: http://localhost/data.php?action=myFunc¶m=123
<?php
function myFunc($param1)
{
echo $param1;
}
if(isset($_GET['action'])){
if(function_exists($_GET['action'])) {
$_GET['action']($_GET['param']);
}
}
how to call a php function from address bar?
You can check presence of GET parameter in URL and call function accordingly:
if(isset($_GET['writeMessage'])){
writeMessage();
}
calling php function by url with no securtiy risk
You are right that the first option is extremely risky, which is why you need to validate user inputs (including GET parameters).
Your second option does exactly that. The code is not perfect but does solve that serious vulnerability.
Best (and secure) way to call javascript function (with arguments) using a url from another page
For example you can append some parameter at the end of your URL
https://your-url/?parameter=hello
When this URL is opened on another webpage you can run JavaScript or a PHP function based on that URL query.
For JavaScript
getUrlParam(slug) {
let url = new URL(window.location);
let params = new URLSearchParams(url.search);
let param = params.get(slug);
if (param) {
return param;
} else {
return false;
}
}
console.log(getUrlParam('parameter'));
How to call php function inside ajax method
Generally you will have files that contain functions/definitions and others that handle requests. (This is not necessary, just common practice)
Files that handle requests will include any relevant functions/definitions from those other files
For this case, let's use functions.php
to contain your main functions and actions.php
as a page to handle requests
See the below setup
// functions.php file
function get_team_members(string $team_name){
// return a list of members based on $team_name
}
// actions.php file
include "functions.php";
$action = $_POST["action"] ?? "";
$data = null;
switch($action){
case "get_team_members":
$data = get_team_members($_POST["team"] ?? "");
break;
}
echo json_encode($data);
// js file
$(document).ready(function() {
$("#your_teams234").change(function() {
var team = $("#your_teams234").val();
$.ajax({
url: 'actions.php', // update the url to request from actions.php
method: 'post',
data: { // update data to reflect expected data
action: "get_team_members",
team: team,
},
})
.done(function(requests) { ... })
})
})
Related Topics
Is There a Limit When Using PHP Mail Function
Increase the Limit of File Upload Size in Heroku While Uploading to Dropbox
How to Get Around or Make PHP JSON_Decode Not Alter My Very Large Integer Values
Remove Attribute Values from Product Variation Title and Show Them on Separate Rows
How to Sum All Values of a Specific Array Key Recursively
Html5 Audio Says "Live Broadcast" in iOS When It's a Static File
PHP Session-Based Flash Message
Strange Echo, Print Behaviour in PHP
Php: Session Lost on Subdomain
How to Parse Xml Containing Custom Namespaces Using Simplexml
Official End of Support for PHP4
Parse Select Clause of SQL Queries into a PHP Array
Get Refunded Orders and Refunded Order Items Details in Woocommerce 3
Loading CSV into MySQL Table with PHP