Jump to content

Writing to $_POST


ShadowMage

Recommended Posts

Is writing to the $_POST variable bad practice? The reason I ask is because I have a script that is normally accessed through AJAX, but I would also like to use require to include the output in a different file. In this script, I check for several $_POST variables and use them in generating the output. If I simply include the file, $_POST will obviously not be set, so I thought I could just set the appropriate $_POST variables before including the file. It works, I'm just not sure if it's a good idea. If this is not the best way to handle this, what are some other options? I have a few in mind, but I want some professional opinions. Thanks.

Link to comment
Share on other sites

I think this touches upon the topic of dependency injection. http://en.wikipedia....dency_injection Ideally, you want to decouple your code as much as possible from any dependancies (in this case $_POST) to help with testing, portability, and maintainability. I'm not sure what the nature of your included file is, but is there any reason you can't make it a function or maybe a static class that you can pass a set of arguments to, or an array in your case, and then call a method on it to get the desired output? I personally don't see a big problem with writing to $_POST manually per se, since it's just a global array, but as you are finding out, you are coupling your code and forcing a dependency onto it. If you manually supply the data through some sort of basic datatype, you can move your code anywhere, and providing you give it the right set of information, it should work anywhere, since it's only dependent on the data you give it directly. edit: from a purely semantic reason, I agree with some of the comments here, in that POST is meant to handle data coming from a form or an HTTP POST request. http://stackoverflow.com/questions/3235265/is-it-bad-practise-to-write-to-postJust like you use certain tags in HTML for a certain reason, it seems to imply a sort of trickery of sorts, as Douglas Crockford would say about certain bad Javaascript habits. Though I don't see anything wrong it since I know you know what you are doing, I personally wouldn't do it.

Edited by thescientist
Link to comment
Share on other sites

Ideally, you want to decouple your code as much as possible from any dependancies (in this case $_POST) to help with testing, portability, and maintainability. I'm not sure what the nature of your included file is, but is there any reason you can't make it a function or maybe a static class that you can pass a set of arguments to, or an array in your case, and then call a method on it to get the desired output?
I'm not really sure that any of those options are feasible. I'll try to explain it a bit more:I have a page (let's call it PageA) with a sidebar containing a long list of items. Clicking one of those items sends an AJAX request with the item ID and a few other parameters as POST data to my script mentioned before. This script generates and returns (echoes) an HTML string which is then used to populate a content pane on PageA with information on the selected item. I would like to be able to add the item ID to the URL of PageA (as a query string) to enable linking/bookmarking of specific items. The script will also be accessed from other pages, but producing different outputs for those pages. The list of items will always be the same and the pages rely on much the same information, so to me it didn't make sense to create multiple scripts for each page.
Though I don't see anything wrong it since I know you know what you are doing, I personally wouldn't do it.
Personally, I don't really like the idea either. Which is why I asked for suggestions... ^_^I suppose it probably doesn't matter too much, anyway, since this is a personal project that will never see the open web, but I still like to keep up on good/bad practices. If I get in the habit of using bad practices, I'll regret it when it really does matter.
Link to comment
Share on other sites

I would prefer code readability in this case. Change your ajax script to detect if there is information in $_POST or if the request is a post request ($_SERVER["REQUEST_METHOD"] tells you if it is a get or post request). Another option is including an extra querystring parameter in the ajax requests to explicitly tell the script that this is an ajax request. If you determine that this is not an ajax or post request, then look for the values that you're expecting from the included file in another array or set of variables somewhere else. That will make sense semantically and will make it pretty explicit what your code is doing, that it can operate either as an ajax post request or an included file. It will add a few extra lines where you're getting the values, but it will make the code easier to understand.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...