0

I been learning php and ajax from w3schools but I have come across a simply question for which I cant find an answer to.

To request something from a php file I use a xmlhttpRequest object and specify the url (of that php file). Does this mean one php file for one request only? Let's say on a webpage there are a user log-in box and a comment box, I would need two php files to take the requests? I always thought the server side will have one main file that handle all the requests, each request from client will have a ID to specify what the request is and send back the necessary data to client. So what is the right?

I read a lot of material online, but everything is just basic example with just one request and one response.

2
  • 2
    w3schools often has wrong information. Be very careful taking it at face value. Especially when you're learning (and then even as a pro) it's much better to use a Javascript framework such as jQuery jquery.com rather than trying to code straight javascript. Finally there is no right answer to your question. You can have multiple files or you can have a single file with conditional logic to do different things depending on the request. Commented Aug 16, 2013 at 0:14
  • Thanks for the advice! I am planning to learn it elsewhere once I get the basic down, it's just that w3school simplify everything so I can learn quicker. Get the basic structure down then start coding Commented Aug 16, 2013 at 0:22

1 Answer 1

1

You can use the same file for multiple requests. You can supply parameters along with the AJAX request, either by including them in the URL after ? (they'll be available in $_GET and $_REQUEST) or by using the POST method and sending them as form data (they'll be available in $_POST and $_REQUEST). You can use the Javascript FormData API to encode this properly; see the documentation here. Using the jQuery library can simplify all of this.

One of the parameters can then be a command or operation code, and the script can take different actions based on this.

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

Comments

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.