Solved

How to embed php function in javascript function.

Posted on 2011-09-06
4
398 Views
Last Modified: 2012-05-12
How to call php function in javascript function on click of html button.
0
Comment
Question by:searchsanjaysharma
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36487049
0
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 36487081
You have to do this through AJAX.

With jQuery it is easy:
 
$.ajax({
		cache: false,
		url: "http://yoursite.com/yourphpfile.php, 
		type: "POST", 
		dataType: "json",
		success: function(data){
			if (data.success) {
				// your actions if successful
			} else {
				// your actions if fail
			}
		} 
   	});

Open in new window


There, in the PHP file you designate for the AJAX action you would place your call to the PHP function of your choice:
 
<?php

         $success = yourPHPFunction();
	$response = json_encode( array( 
		'success' => $success
	));
	

	header( "Content-Type: application/json" );     
	echo $response;
	exit;
?>

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
ID: 36487094
Thanks
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36487113
No problem.  Best of luck to you on your project!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now