alexweber15 Posted April 29, 2009 Share Posted April 29, 2009 sup all, lately I've been to use OOP in my PHP work and it's definitely made my code more reusable and elegant but there's one thing that I still do the same as before and it kinda bugs me... submitting forms. I always end up having a file called "submit.php" that collects POST data and redirects the script flow to the correct class/method combination. And when I use AJAX, all the file does is: die($_SESSION['myObj']->myMethod($_POST[...]); (the functions all return JSON objects) All this doesn't seem too OOP and elegant at all to me. What's the convention for handling form submits with OOP? And also is there a more elegant way to return JSON objects ? (can you do like content-type="text/json"?") thanks Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/ Share on other sites More sharing options...
alexweber15 Posted May 2, 2009 Author Share Posted May 2, 2009 um.... bump? Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-824535 Share on other sites More sharing options...
alexweber15 Posted May 13, 2009 Author Share Posted May 13, 2009 i guess this subject must be taboo or something (last bump) Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833408 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 And when I use AJAX, all the file does is: die($_SESSION['myObj']->myMethod($_POST[...]); (the functions all return JSON objects) All this doesn't seem too OOP and elegant at all to me. What's the convention for handling form submits with OOP? Not sure what you mean by "conversion for handling form submits with OOP." And also is there a more elegant way to return JSON objects ? (can you do like content-type="text/json"?") JSON is JavaScript. Just output it as JavaScript code. PHP does have json_encode and json_decode functions. Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833414 Share on other sites More sharing options...
alexweber15 Posted May 13, 2009 Author Share Posted May 13, 2009 sorry i meant conVention!! and yeah I know, I do use json_encode() but then I end up doing a die() with the encoded values in order to send them back to my javascript and that just doesn't seem that elegant but maybe its the way. I figured doing something like Header('Content-type: application/json') would be more approriate but it doesn't seem to work. Alex Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833578 Share on other sites More sharing options...
Philip Posted May 13, 2009 Share Posted May 13, 2009 JSON is normal text - just formatted for computers to read it more efficiently. Example of json: {"widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, "image": { "src": "Images/Sun.png", "name": "sun1", "hOffset": 250, "vOffset": 250, "alignment": "center" }, "text": { "data": "Click Here", "size": 36, "style": "bold", "name": "text1", "hOffset": 250, "vOffset": 100, "alignment": "center", "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" } }} You'd output it just like you would for a normal user, no content type needed. Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833582 Share on other sites More sharing options...
alexweber15 Posted May 13, 2009 Author Share Posted May 13, 2009 thanks for the reply, gotcha, that's what I've been doing it but I just always figured there was a better way to do it... guess not! Since I seem to have your attention mind taking a look at my original question about submitting forms? In a nutshell: When programming with classes I usually end up having a handful of classes and the regular PHP/HTML pages that take care of user interactions and I always end up creating a file called 'submit.php' that's a central place to direct all form submissions, route them to the appropriate classes/objects and then return flow back to the user (which is where I would output JSON in the case of AJAX stuff) I never liked the whole 'submit.php' thing but can't think of a better way to do it... is there any? thanks Alex Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833614 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 I think it's okay unless your submit.php was just: <?php if(isset($_POST['foo'])) { switch($_POST['foo']) { case 'bar': $class1->submit(); break; case 'joe': $class2->submit(); break // ... } } ?> Are you using any specific pattern on your scripts? Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833676 Share on other sites More sharing options...
sKunKbad Posted May 14, 2009 Share Posted May 14, 2009 I've been using the Kohana framework, and also CodeIgniter framework. I have my forms post back to the same controller that produced them, run some validation if the post token = session token, and if all is well I use cURL to post to a processing script that returns a confirmation or error message, which is then pushed into the view. If there were errors in validation, the cURL post is not done, and errors messages are pushed into the view, and the form is prefilled with the data that passed validation. If the post token != session token, the user simply gets the standard view, sans any prefilled form data. I could send you my contact controller file if you want to see, but I don't want to post it here. Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833680 Share on other sites More sharing options...
alexweber15 Posted May 14, 2009 Author Share Posted May 14, 2009 I've been using the Kohana framework, and also CodeIgniter framework. I have my forms post back to the same controller that produced them, run some validation if the post token = session token, and if all is well I use cURL to post to a processing script that returns a confirmation or error message, which is then pushed into the view. If there were errors in validation, the cURL post is not done, and errors messages are pushed into the view, and the form is prefilled with the data that passed validation. If the post token != session token, the user simply gets the standard view, sans any prefilled form data. I could send you my contact controller file if you want to see, but I don't want to post it here. Thanks for the reply! Yeah I'm curious to see it if you wouldn't mind sharing... Alex Quote Link to comment https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/#findComment-833734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.