Geo
Geo

Reputation: 3200

Accessing PHP function from an ajax call

I am trying to access a PHP function from an ajax call but I am running into all sorts of issues since is my first time doing something like this. I need help creating the function or fixing the one I have.

This is my ajax call (I will only post a small part of it since the complete function is irrelevant to the question)

 $.ajax({
    type        : 'post', 
    url         : 'data.php?action=checkname',
    data        : {'location':locationName},
    error       : function(data,status,error){
                console.log(data+': '+status+': '+error);
            },
    success     : function(res){
                console.log(res);
            }                   
    });

PHP function

<?php
    function checkname(){   <--- What do I pass in the function???

        $locations = $_POST['location'];

        echo $locations;

    }
?>

If I do this the "simple way" by just calling the file and do something like

$locations = $_POST['location'];

echo $locations;

I get the return that I need but it will be wrong to just have a small file for all the ajax calls that I need to create.

Upvotes: 0

Views: 92

Answers (4)

Panama Jack
Panama Jack

Reputation: 24458

You can setup for php doing something like this. Use a Switch and give each action a name in your ajax the name of the Case you want to activate. This way you can use the same file and call different functions as you see fit.

<?php
            $action = $_POST["action"];
            //check if it was sent using $_GET instead of $_POST 
            if(!$action)
            $action = $_GET["action"];

            switch($action){
                case 'checkname':
                 checkname();
                break;

                case 'otherfunction':
                 otherfunction();
                break;

             }//switch


     function checkname(){   

                $locations = $_POST['location'];

                echo $locations;

     }

     function otherfunction(){  

                //do something else

                //echo something;

     }
?>

I didn't put the ajax because you already had the ajax call. This part of your ajax is the name you will use for the action. data.php?action=checkname or you can use the data like this.

var action = 'checkname';

$.ajax({
    type        : 'post', 
    url         : 'data.php',
    data        : {'location':locationName,'action':action},
    error       : function(data,status,error){
                console.log(data+': '+status+': '+error);
            },
    success     : function(res){
                console.log(res);
            }                   
    });

You can use other ajax functions and just change variable action to the function you want to call.

Upvotes: 1

Sergei Beregov
Sergei Beregov

Reputation: 738

Add some conditions to file:

$action = isset($_GET['action']) ? $_GET['action'] : null;

switch($action) {
   case 'checkname':
       checkname();
   break;
   case default:
    // default action
   break;
}

Upvotes: 1

Simone Nigro
Simone Nigro

Reputation: 4887

$functionName() or call_user_func($functionName)

$action = (isset($_POST['action'])) ? $_POST['action'] : null;

if ( !empty($action) && function_exists($action) )
    call_user_func($action);

Upvotes: 0

Vikas Duhan
Vikas Duhan

Reputation: 91

you can code the php content like this:

  <?php
/*this part of code will be executed only when location parameter is set by post,
thus can send multiple ajax requests to a single file*/
  if(isset($_POST['location'] && $_POST['location'] != "")  
  {
     $location = $_POST['location'];
     echo checkname($location);
  }

function checkname($location) 
{
  return $location;
}

Upvotes: 0

Related Questions