Wednesday, March 14, 2012

Call PHP Function In JavaScript Using Ajax

how to call php function from javascript function


1. create Folder on WWW Or htdocs (on your computer)

2. create index.php page on your folder and paste this code


index.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 



<script>
function addsearchproduct(x){  
// this is JavaScript  function  


$.ajax({
  type: 'POST',
  url: "function.php?f=phpFunction&p="+x, 

       // call php function , phpFunction=function Name , x= parameter 
  data: {},
  success: function(data1){
        alert(data1);
  }
});
           

}
</script>

<input type="button" value="call Php Function" onclick="addsearchproduct('pass value');"  />

 



3. create function.php  page on your folder and paste this code

 function.php 

<?php 
if(function_exists($_GET['f'])) { // get function name and parameter  $_GET['f']($_GET["p"]);
} else {
echo 
'Method Not Exist';
}


function 
phpFunction($val=''){      // create php function here  
echo $val ;
 
 }
 
 



4. run your index.php file



 

12 comments:

  1. Aw, this was a really nice post. Spending some time and actual
    effort to generate a superb article… but what can
    I say… I procrastinate a whole lot and don't manage to get nearly anything done.

    my web page - pikavippi 200e

    ReplyDelete
  2. Article writing is also a excitement, if you be acquainted with afterward you can
    write if not it is difficult to write.

    Take a look at my webpage visit the following web site

    ReplyDelete
  3. for me it is displaying "Method Not Exist" message. it is not showing any method name. could please suggest me, how to call php function in javascript

    ReplyDelete
  4. Fantastic post however I was wondering if you could write a litte more on this subject?
    I'd be very grateful if you could elaborate a little bit further. Many thanks!

    Look into my web site: http://nitrofocusfacts.com

    ReplyDelete
  5. It's not my first time to pay a visit this website, i am visiting this web site dailly and obtain good information from here every day.

    Also visit my web page - Buy Xtrasize

    ReplyDelete
  6. I've been browsing online more than 3 hours today, yet I
    never found any interesting article like yours. It's pretty
    worth enough for me. Personally, if all webmasters and
    bloggers made good content as you did, the net will be much more useful than ever before.


    Look at my web page Chicago SEO Services

    ReplyDelete
  7. nice "workaround" - thanks

    ReplyDelete
  8. Very interesting critique

    Also visit my website ... hytera accessories

    ReplyDelete
  9. Somebody necessaгily һelp to make severely posts І'ԁ state.
    That іs the fіrst tіme I frequented үour website рage and to thiѕ point?

    I amazed wіth tһe reѕearch үou made to create tһis actual put ᥙp incredible.
    Magnificent process!

    ReplyDelete
  10. Keep on writing, great job!

    ReplyDelete

If you Satisfied , Please Leave a comment