JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 29th, 2003, 09:39 AM
Evil_homer Evil_homer is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 5 Evil_homer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old April 29th, 2003, 09:01 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
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.
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #3  
Old April 30th, 2003, 06:05 AM
Evil_homer Evil_homer is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 5 Evil_homer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Right, thanks for the heads up.

Reply With Quote
  #4  
Old August 16th, 2004, 03:24 PM
Tjerk Tjerk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 Tjerk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb Calling php methods from javascript is possible

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

Reply With Quote
  #5  
Old September 2nd, 2004, 08:15 AM
nvdberge nvdberge is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 1 nvdberge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #6  
Old September 2nd, 2004, 08:51 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 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-

Reply With Quote
  #7  
Old March 19th, 2005, 01:24 PM
hack hack is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 1 hack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 52 sec
Reputation Power: 0
Smile

Quote:
Originally Posted by kode_monkey
"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-

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.

Reply With Quote
  #8  
Old April 13th, 2008, 01:29 PM
hasantayyar hasantayyar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: Turkey
Posts: 4 hasantayyar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 11 sec
Reputation Power: 0
Send a message via Google Talk to hasantayyar Send a message via Skype to hasantayyar
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..

Reply With Quote
  #9  
Old April 25th, 2008, 05:39 PM
Mittineague's Avatar
Mittineague Mittineague is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 549 Mittineague User rank is Private First Class (20 - 50 Reputation Level)Mittineague User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 7 h 3 m
Reputation Power: 4
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>
This will "work", but it is not javascript calling a PHP function.
__________________
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.

Reply With Quote
  #10  
Old April 26th, 2008, 03:03 AM
hasantayyar hasantayyar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: Turkey
Posts: 4 hasantayyar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 11 sec
Reputation Power: 0
Send a message via Google Talk to hasantayyar Send a message via Skype to hasantayyar
i see
but this will be usefull for short actions...

for example our php function is
PHP Code:
function doit($w){
swicth($w){
case 
'do': echo "you will be logged in?";
case 
'loguot': echo "you will be logged our?";
}




and so we can use this function like this maybe:

PHP Code:
<?
$w
=""
...... 
actions about w;
?>

<script type="text/javascript">
function test(){
document.getElementById("php_code").innerHTML="<?php 
// the doit function's content here
swicth($w){
case 
'do': echo "you will be logged in?";
case 
'loguot': echo "you will be logged out?";
}
?>";
}
</script>

<a href="#" onclick="test(); return false;"> test </a>
<span id="php_code"> </span>


and yes you are right about client/server side codes. But about simple solutions this will be helpful.

Reply With Quote
  #11  
Old May 2nd, 2008, 09:49 PM
timidwind timidwind is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Location: AZ
Posts: 5 timidwind User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 24 m 11 sec
Reputation Power: 0
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?

Reply With Quote
  #12  
Old May 2nd, 2008, 11:46 PM
Mittineague's Avatar
Mittineague Mittineague is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 549 Mittineague User rank is Private First Class (20 - 50 Reputation Level)Mittineague User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 7 h 3 m
Reputation Power: 4
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?

Reply With Quote
  #13  
Old May 2nd, 2008, 11:59 PM
timidwind timidwind is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Location: AZ
Posts: 5 timidwind User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 24 m 11 sec
Reputation Power: 0
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.

Reply With Quote
  #14  
Old May 3rd, 2008, 12:02 AM
timidwind timidwind is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Location: AZ
Posts: 5 timidwind User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 24 m 11 sec
Reputation Power: 0
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);

Reply With Quote
  #15  
Old May 3rd, 2008, 02:30 PM
Mittineague's Avatar
Mittineague Mittineague is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 549 Mittineague User rank is Private First Class (20 - 50 Reputation Level)Mittineague User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 7 h 3 m
Reputation Power: 4
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?

Reply With Quote
  #16  
Old May 6th, 2008, 07:33 AM
timidwind timidwind is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Location: AZ
Posts: 5 timidwind User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 24 m 11 sec
Reputation Power: 0
Yes n no, ok now I'm confused with that!

Quote:
Originally Posted by Mittineague
So if I'm correct, it goes some