www.webdeveloper.com
Results 1 to 6 of 6

Thread: Calling a PHP function using AJAX, how?

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Location
    Gujarat, India.
    Posts
    35

    Calling a PHP function using AJAX, how?

    Welcome!
    I am not sure if this is right section (because of AJAX section)....

    I have 1 php file - "functions.php" containing many functions like add(), edit() etc.

    But IDK how to operate'em using AJAX HMLHttpRequest()....actually I M confused at
    Code:
    send()
    ...

    Here are files, let me know how to operate functions using AJAX and yes... parameters are required. And yes....POST method is required too.

    functions.php:
    PHP Code:
    function add($name) {
    echo 
    $name.' add function is working'//Just to test
    };

    function 
    edit($name|) {
    echo 
    $name.' edit function is working'//Just to test
    }; 
    beta.html:
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    // variable named XMLHTTP is used for creating XMLHttpRequest();
    </script>
    </head>
    Sorry if i didn't explained well enough, hope you will get it.
    Thanks in advance. :D

  2. #2
    Join Date
    Nov 2012
    Posts
    24
    Here is an example, I hope it helps, other than that you can read some tutorials on ajax and understand it better.


    beta.html
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
           // use the xmlhttp.responseText however you need.
        }
      }
    xmlhttp.open("POST", "functions.php?name="+somename, true);   //somename is whatever you want to send in there as name
    xmlhttp.send();
    }
    </script>
    </head>
    The loadXMLDoc() can be called when a button is clicked or when someone types something in a text field or even with a timer that calls it every few seconds. Just like any other javascript function.

    functions.php:
    Code:
    function add($name) { 
    echo $name.' add function is working'; //Just to test 
    }; 
    
    function edit($name|) { 
    echo $name.' edit function is working'; //Just to test 
    };  
    
    if(isset($_POST['name'])){
    $name=$_POST['name'];
    
    add($name);
    edit($name);
                      //whatever other function you want to call
    }
    Last edited by darkylord; 11-11-2012 at 09:19 AM.

  3. #3
    Join Date
    Oct 2012
    Location
    Gujarat, India.
    Posts
    35

    It helped but....

    Thanks, your answer helped but..

    $_POST['name'] isn't being set...why?

    Here are files......
    Code:
    /* AJAX */
    function AjaxFunction()
    {
    var somename = "somenameTest";
    
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
           document.getElementById("txtHint").innerHTML = xmlhttp.responseText; // use the xmlhttp.responseText however you need.
        }
      }
    xmlhttp.open("POST","gameFunctions.php?name="+somename, true);   //somename is whatever you want to send in there as name
    xmlhttp.send();
    alert("Script loaded");
    }
    Here is gameFunctions.php
    PHP Code:
    function add($name) {
    };
    echo 
    "php data<br>";
    if(isset(
    $_POST['name'])) {
    echo 
    "true";
    }
    else {
    echo 
    "false";

    And Output....


    php data
    false

  4. #4
    Join Date
    Nov 2012
    Posts
    24
    My bad, the $_POST['name'] should be $_GET['name'] .

  5. #5
    Join Date
    Oct 2012
    Location
    Gujarat, India.
    Posts
    35

    Really....

    Are you sure?

  6. #6
    Join Date
    Nov 2012
    Posts
    24
    Yes I tested the code and worked fine with $_GET['name'] .

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles