2

I would like to build a web app using json, ajax and php to be more clear, I need a php page with different functions that can be called from ajax method

Eg: phppage.php

addmydetails() 
{
   logic goes here
}

ajax page

/* Get from elements values */
 var values = $(this).serialize();

 $.ajax({
        url: "phppage.php/addmydetails",
        type: "post",
        data: values ,
        success: function (response) {
           // you will get response from your php page (what you echo or print)                 

        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }


    });

So my actual question is that any option to call the function like this in ajax post url: "phppage.php/addmydetails"

3
  • htaccess redirect calls to phppage.php there check whats /addmydetails and execute the function Commented Sep 10, 2018 at 7:04
  • Well you can, you post a value to specify which function to use. Commented Sep 10, 2018 at 7:04
  • I feel the question is more about URL re-writing and its implementation in PHP, this has nothing to do with AJAX. Commented Sep 10, 2018 at 7:05

1 Answer 1

3

No need for URL rewriting. Just get the PATH and use call_user_func. Something like this:

In your phppage.php:

$method = trim($_SERVER["PATH_INFO"], "/");

if(function_exists($method)){
    call_user_func($method);
    exit();
}

function Your_function_name(){
    print "this is my function";
}

Then you can access it using this URL --> phppage.php/Your_function_name

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

5 Comments

I think OPs question is more on how to access phppage.php/Your_function_name as an URL
isn't it what my answer is? using the code you can access it using phppage.php/Your_function_name
I think parpar's answer do the trick, kind of very basic url dispatching, lol
glad to help :D
Thank you so much parpar for the timely help:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.