0

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.

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I still don't see how you are sending in the information to the function besides checking what function you need to run...or you don't need to send in anything?
1

Add some conditions to file:

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

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

Comments

0

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;
}

2 Comments

I thought of this method too but did not seem ideal for some reason due to the extra lines of code that I have to write to accommodate all options
there will be only one if condition for the complete ajax request just like a checksum and then all the code for that ajax will be inside it as normal, or kindly clarify the issue.
0

$functionName() or call_user_func($functionName)

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.