Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/156182-submitting-forms-oop-style/
Share on other sites

  • 2 weeks later...

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. :)

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

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.

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

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?

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.