jquery call for a remote php function - php

I have a selectbox that needs to be updated dynamically. I am using jquery to perform that using .load($url), where $url is the location of the php file to call. I am wondering if there is a way to call a specific function within that php file instead of calling the entire php file.

You could send a GET parameter as part of the AJAX request e.g. blah.php?func=1
Then in your PHP file:
if ($_GET['func'] == 1) {
do_something();
}
else {
do_something_else();
}
Or you could use something like CodeIgniter that structures its URL's as controller/function/parameter

no, you can't. but you can pass GET parameters to the URL, and use that URL as a controller.
for example:
.load("/ajax_controller.php?action=whatever")

You can pass a parameter to PHP script and user switch in script to select which action needs to be done

Related

javascript value pass to php

Is there a way to pass javascript value to php?
if(echo '<script>window.location.href="/mysite"</script>' === "/mysite"){
$myVal = 1;
}else{
$myVal = 2;
}
This way seems doesn't work
Nope you can't do it. If you want something like that best and easiest way is using JQuery ajax function http://api.jquery.com/jQuery.ajax/ which will send request to server with data you want to given script (easiest way - make other .php file?) and if you want something returned, it will be in ajax returned data. Read provided link and you should be good to go.
If you want to know on which site you are currently (as in example) you can use $_SERVER array and get variable which you want from it. List is here: http://php.net/manual/en/reserved.variables.server.php
PHP code executes server-side. JavaScript executes client-side. There's no way for the two to communicate unless you create a web service in PHP that is called from the JavaScript. Unfortunately that won't solve your problem.
If you're trying to set values based on the requested url, you could try:
if($_SERVER['REQUEST_URL'] === "/mysite"){
$myVal = 1;
} else {
$myVal = 2;
}
The code I see in your post shows that you completely misunderstand PHP and Javascript nature.
Send JS var through AJAX request and then work with it in some server-side script.
php is executed on the server side first.
then javascript is executed on the client side. So it's after the php has been rendered.
The only way to send javascript values to php, is using ajax to send an asynchronous call.

AJAX Load function from php

;-) I have a hit a brick wall. Today I have met Ajax for the first time in my life. And I understand, that my question can seem very stupid. But I need your help.
I have a php file (for ex. index.php), it has an include (function.php). In the file function.php I have a function (for example function Jokes(){....}).
I want to use AJAX to load this function in my page. But I don't know how to do this... :-(
I have found some easy solutions (I use Jquery load function), but it's not what I want, because I have to use a separate file.
$('#jokes').load('jokes.php');
But I need something like:
$('#jokes').load('function.php','jokes()');
Any help or ideas? It will be appreciated.
P.S: Sorry for my bad English...))
What you can do is make one AJAX file, e.g. ajaxfunctions.php where you divide the functions.
$('#jokes').load("ajax.php", { 'function': 'jokes' } );
In your PHP file ajax.php you can do this:
<?php
include 'function.php';
if (isset ($_POST['function']) && $_POST['function'] == 'jokes')
{
echo jokes ();
}
?>
Does this help you?
The browser (and thus Ajax) can only make HTTP requests to the server.
It cannot execute specific functions.
You would have to write server side code that would recognise a particular URL (e.g. to a script that only calls that function, or a script that decides which function to run based on the query string) and execute a particular function in response to a request to it.
No, it's impossible to do it this way. You must make a request to a url. You can't just invoke a specific function from a php file.
Build a url wrapper for the function you need.
Don't do that.
Ajax is used to send or retrieve data, but what you are trying to do is to load a Javascript function defined in a PHP file (isn't it?).
Instead, just define your Javascript function in a JS file and load it via a script (or, if it should be loaded dynamically, you may use an AMD loader like require.js)
If you want to load a PHP function in JS, then it's not possible. You may call that function and fetch its result instead, though.
It doesn't work like this.
$('#jokes').load('jokes.php');
does an http request to jokes.php. If you want to execute a specific function, you can use a parameter, like this:
$('#jokes').load('jokes.php?func=jokes');
From your php script, get 'func' and execute the proper function.
...
function jokes() {
return 'some output';
}
...
$func='';
if (!empty($_REQUEST['func'])) {
$func=$_REQUEST['func'];
}
if ($func=='jokes') {
echo jokes();
}
You don't need AJAX for this. You want to put the Jokes() function in a JavaScript file, maybe called Jokes.js, include it in index.php and then call it normally in a JavaScript block in index.php.

JQuery - passing variable to php script

im stucked with following issue. I have an javascript (jquery) function where i need to pass a variable to php ON .success method. See below Please:
http://paste.org/55404
Any idea how to achieve this? Or is it even possible?
Thanks in advance.
jQuery runs in the browser. PHP runs on the server. So you can not mix and match them like that.
Instead, you will have to use AJAX or another mechanism to contact the server and get the information you need.
Depending on your use case, there may be other solutions, such as inserting the values into the page when the server generates it, or putting the values in a cookie.
Referring to your code, The .success method is called, when your ajax request to "index.php" returns a successful response.
Since you need to get the PHP variable in this function, you can attach the value of "genNavSide()" to the response as a parameter. It is the same way you assigned pageName value, and content value to the response data.
For example, instead of :
<?php echo pagesSideBar('data.pageName') ?>
call the function from your index.php, during the ajax call.
$sidebar = pagesSideBar($PageName);
$response->sidebar = $sidebar; // I just mentioned object, you can assign whatever way you are following to create the response
In the .success method :
jQuery('#links').html(data.sidebar);

AJAX query fails due to missing DEFINE variable in PHP

I am new to AJAX/jQuery with PHP.
I am trying to call a PHP script via AJAX using XMLHttpRequest or jQuery. In both cases the call fails because the php file I am calling into contains on the very first row the following check
if (!defined("SOMETHING")) { die("Error. You cannot access this file directly");}
which causes that my call fails, because this variable is not defined in this case as I am calling from the outside. This condition just checks if the caller is the same web application or wheather the call comes from outside (then it will be denied).
Is there a workaround for it without removing this check? Can I somehow set this expected variable using AJAX/jQuery?
Is there a way how to call specific PHP method via AJAX without calling into the whole PHP file?
Thanks in advance
Tomas
Hmm... you can do it, but I am not sure if this is secure or the way you would like it to be.
Your jQuery should post a variable with GET or POST, which you should check on PHP side. If you have received that variable, then define SOMETHING.
Here is your jQuery, using the POST method:
$.post('ajax.php', {SOMETHING: true}, function(ret){
// do whatever you like with the return here
});
Here is your PHP, which will define SOMETHING if it receives a $_POST request with the variable SOMETHING in it.
<?php
if(isset($_POST['SOMETHING'])){
define('SOMETHING', true);
}
if (!defined("SOMETHING")) { die("Error. You cannot access this file directly");}
// do anything you like here
?>

JQuery .GET call/function

Ok so trying to get a page together that needs to connect to our SQL database. So on the javascript page I have functions that will autocomplete a textbox with data out of our mysql DB and then I need to send it to other functions and classes so that it will then look in our SQL DB and return some data. The problem I have is trying to get the .GET call to call in the php page, with the function that calls the class in which I need to get into for the SQL call. I have it setup somewhat but trying to figure out how to send the data through with it as well as just get clarification on how to work the .GET function.
Javascript page:
$.get("dsl_validate.php", calldsl(job));
Php Page
function calldsl($job){
var $dsljob = $job
hasfunctioncode($dsljob);
}
The hasfunctioncode function is in my DSL class page that will return the info I need. Any help on if I am in the right direction or not?
It looks like you're trying to physically call the PHP function calldsl() from the JavaScript. This... isn't right. (I'm assuming the $.get() you're using is from jQuery, please correct me if that assumption is incorrect.)
What $.get() does is simply call a resource on the web server. It doesn't have any knowledge of the server-side code (nor should it, for a number of reasons). From the perspective of the server-side code, there's no difference between a page being called via $.get() vs. one that's just loaded in a web browser.
What you essentially need to do is create a PHP page which accepts arguments either as a form post or query string (if you're using $.get() then the query string is the way to go), does its server-side logic, and then simply outputs the results to the "page." In the case of calling the page via AJAX as you are here, it's a good idea to render the page content using JSON notation. (Don't forget to set the content-type header to "application/json" as well.)
Then what you're getting on the client-side from the $.get() call is the response body, which would be that JSON data. It's really just a "page" like any other, the only difference is the content-type telling the browser that it's JSON data and that it doesn't have HTML, just JavaScript objects. The success callback on the $.get() call (the function you pass it, or create in-line) would receive that response data as an argument and can do what you need to with it.
The way I understand jQuery.get(), the second argument is the "callback" (http://api.jquery.com/jQuery.get/). The callback will hand the results from your server therefore should be a function. Currently your code actually executes the function "calldsl" where you should be only passing a reference like so...
Javascript:
$.get("dsl_validate.php", function(response){
alert("yay I haz ajax! "+response)
});
PHP: "dsl_validate.php"
echo "this is some data from the server";
No, your are not in the right direction. The first parameter of the get method have to br the complete URL of the page, not just the script (this works if the script resides on the same directory of the javascript file, though). The .php file shall return somehting "usable" for you javascript (JSON, or HTML, or text, or... whatever). The "calldsl" function will be called AFTER the data has been returned from the call. Something like that:
$.get('dsl_validate.php?value=somevalue', function(data) {
alert("Data returned from dsl_Validate: " + data)
});
i think you are better off passing the function as a param to your php page
$.get("dsl_validate.php?calldsl="+job, function(data) {
$response = $(data);// create a jquery object from the response
});
`
and in your php file
create a switch statement that call the function based on the parameter
Mmm I think you are wrong, the second argument on your get function is the javascript function that will process de data returned by "dsl_validate.php". I mean, if that page returns "foo", job will contain "foo".
But in my experience is better to use the autocomplete plugin from Jquery UI
jquery autocomplete plugin

Categories