|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Calling php functions with JavaScript
Thanks to asgeo1 and stumpy for answering my previous JavaScript related query. However i've now come across another problem:
Is there any way to call a php function, or execute some php code using JavaScript. I've searched on google for a while but found no real solution. Basically i use a form to call a javascript function, then i want that javascript to call a php function found within the same page, or failing that be able to execute a few lines of php script from within the javascript. Any help greatly appreciated ![]() |
|
#2
|
||||
|
||||
|
Sounds like you're getting confused with developing on the web.
Javascript (unless specified) is a "client-side" language, meaning that it runs on the clients browser AFTER has page has been compiled and sent from the server. This means that there is no PHP/ASP in the code, only HTML & Javascript. PHP, ASP, JSP are server-side languages which only run on the server-side. So to answer your question, no you can't directly get the Javascript to call a PHP (server-side) function, but you can post to a PHP page which will then in turn carry out whatever function u like. |
|
#3
|
|||
|
|||
|
Right, thanks for the heads up.
|
|
#4
|
|||
|
|||
|
calling php methods from javascript is possible by using xmlrpc (see xmlrpc.com) you can call a php methods remotely with javascrit by setting up a xmlrpc server (a php page) and then acces that server within javascript. There are php xmlrpc libs and javascript xmlrpc .js files.
in computersceince everything is possible ![]() |
|
#5
|
|||
|
|||
|
A good working solution I think
Hi,
I wrote this code. The comments say enough I think. Code:
<HTML>
<BODY>
<?php
if (isset($_GET[action])){
// Retrieve the GET parameters and executes the function
$funcName = $_GET[action];
$vars = $_GET[vars];
$funcName($vars);
} else if (isset($_POST[action])){
// Retrieve the POST parameters and executes the function
$funcName = $_POST[action];
$vars = $_POST[vars];
$funcName($vars);
} else {
// If there is no action in the URL, then do this
echo "<INPUT NAME='btnSubmitAdmin' TYPE='button' ONCLICK='javascript:javaFunction()' VALUE='Call Javafunction() which redirects to a PHP function'>";
}
function phpFunction($v1){
// makes an array from the passed variable
// (note: $vars = 1 string while it used to be a javascript Array)
// with explode you can make an array from 1 string. The seperator is a ,
$varArray = explode(",", $v1);
echo "<BR>function phpFunction<BR><BR>";
echo "v1: $varArray[0] <BR>";
echo "v2: $varArray[1]<BR>";
}
?>
<SCRIPT language="javascript">
function javaFunction(){
// In the varArray are all the variables you want to give with the function
var varArray = new Array();
varArray[0] = "var1";
varArray[1] = "var2";
// the url which you have to reload is this page, but you add an action to the GET- or POST-variable
var url="<?php echo $_SERVER[PHP_SELF];?>?action=phpFunction&vars="+varArray;
// Opens the url in the same window
window.open(url, "_self");
}
</SCRIPT>
</BODY>
</HTML>
regards, Niels |
|
#6
|
|||
|
|||
|
"In computer science everything is possible"
An interesting stand point to take... would love to see a polynomial time algorithm for some of the famous NP problems. -KM- |
|
#7
|
|||
|
|||
|
Quote:
Patience is a virtue KM, the "famous" NP hard problems may be NP hard now but not necessarily in the future therefore kode_monkey is right. |
|
#8
|
|||
|
|||
|
The simple solution for "How can i call PHP functions from Javascript?"
The simple solution for "How can i call PHP functions from Javascript?"
(i didnt test on i.e. or opera... i tested this on firefox!) Here you dont need to use XMLHttpRequest or any other complex things. Code:
// .... here some php lins... variables o some functions....
//... maybe some post variables an some new values.... proccessed over post variables....
<script type="text/javascript">
function test(){
document.getElementById("php_code").innerHTML="<?php for($i=0; $i<10; $i++) echo $i; //maybe a function ?>";
}
</script>
<a href="#" style="display:block; color:#000033; font-family:Tahoma; font-size:12px;" onclick="test(); return false;"> test </a>
<span id="php_code"> </span>
thats all.. |
|
#9
|
||||
|
||||
|
javascript PHP example
Hi hasantayyar, welcome to the forums.
Actually, what you posted is not an example of javascript calling a PHP function, but rather using PHP to populate a javascript function. Javascript is client-side and PHP is server-side, so the PHP will run before the page is sent to the browser. In this case, the page's source code will be Code:
<script type="text/javascript">
function test(){
document.getElementById("php_code").innerHTML="0123456789";
}
</script>
<a href="#" style="display:block; color:#000033; font-family:Tahoma; font-size:12px;" onclick="test(); return false;"> test </a>
<span id="php_code"> </span>
__________________
WP plugins - Error Reporting, Clean Options http://www.mittineague.com/dev/er.php http://www.mittineague.com/dev/co.php Last edited by Mittineague : April 25th, 2008 at 05:41 PM. |
|
#10
|
|||
|
|||
|
i see
![]() but this will be usefull for short actions... for example our php function is PHP Code:
and so we can use this function like this maybe: PHP Code:
and yes you are right about client/server side codes. But about simple solutions this will be helpful. |
|
#11
|
|||
|
|||
|
What about activating a javascript function from php? I bet I'm gonna hear about server/client side etc....
Here's the deal: Have html form, as user enters need to validate fields. Get values of phone and cell with onBlur activating javascript, which Sends values to inlaid php testing, now just need to call a javascript alert and keep user in entry loop until the phone #'s are valid and different ??? Whew.... Anyone? |
|
#12
|
||||
|
||||
|
validate input
Hi timidwind, welcome to the forums.
I'm a bit foggy. What exactly do you want the PHP to do with the javascript code? |
|
#13
|
|||
|
|||
|
Provide an alert pop-up; alerting the user that either (both situations arrived via php tests) a) they have entered either their phone or cell number in the wrong format OR b) both #'s are the same; ...please repair your entries. I have successfully used Javascript to do a simple compare between the two form entries, alert the user and prevent the user to proceed until they're fixed (loop if you will); however that's the only condition tested for within the script. In order to trim, check formatting, and then compare I had to send the values out to php from a javascript capture from an 'on blur' prior to form 'submit'. But now, I've tested them, I need an alert as well as to not proceed until they're fixed.
I can't believe I'm having this much trouble with it though. It seems such a simple concept, to alert the user to entry problems as they happen before submission then receiving a laundry list of problems or re-entries. |
|
#14
|
|||
|
|||
|
Am so sorry, I am remiss that I forgot to type a thank you for the welcome first thing to you Mittineague; That was oh too rude. I am so sorry.
Thank You for your interest, and anyone who's willing to help. vbmenu_register("postmenu_196818", true); |
|
#15
|
||||
|
||||
|
validation logic
So if I'm correct, it goes something like:
1: Form page loads 2: User enters data 3: Javascript validates input client-side 4: If not OK shows javascript alert(), else if OK (js-wise) makes (AJAX ??) request to server to check validity server-side 5: If OK server-side do stuff, else if not OK "alert" user of error I guess to "hold up the alert() until they're fixed", you could test the original input value (the bad one) against the "current" or "new" one to make sure they're differnt and then check server-side again. Is this what you mean? |
|
#16
|
|||
|
|||
|
Yes n no, ok now I'm confused with that!
Quote:
|