How to embed php function in javascript function.

How to call php function in javascript function on click of html button.
searchsanjaysharmaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
OmniUnlimitedConnect With a Mentor Commented:
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
 
Gurvinder Pal SinghCommented:
0
 
searchsanjaysharmaAuthor Commented:
Thanks
0
 
OmniUnlimitedCommented:
No problem.  Best of luck to you on your project!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.