tags:

views:

49

answers:

3

Hi All,

Is it possible to send form data to a PHP class' method rather than to a file? Or would I have to use AJAX to do this?

For instance, can I have something like:

<form action="my_obj::form_submit" method="post">...</form>
//rather than
<form action="my_obj.php" method="post">...</form>

I don't want anyone to be able to access the entire file directly. I don't see a problem having a single public static method that is available, though, because a single method is easy to "harden" for public use. There is an AJAX function to help me do this, but I think it would be faster to not do that.

Thanks

+1  A: 

No, it's not possible as the client-side code can never know about that kind of server-side implementation detail. As Chuck Vose comments, if that were even possible, it's a major security liability.

action attributes on <form> elements, like <a href>, are simply URIs, so you cannot point to some specific part of a PHP code and expect PHP to assemble everything and run it. Again it's the same client/server side issue.

Unless you use some kind of URL routing like many frameworks provide (per webbiedave's answer), you won't be able to go any further than a URI pointing to a PHP script in your site.

BoltClock
A: 

I would try to handle it via a specific uri query. So you can check, whether a key in the $_GET variable is given and load the class with a specific method.

Otherwise knowing of a given key in the $_POST variable let you forward your process to the method you want to call. So there is no specific need for this kind of form submit call.

Lemmi
A: 

You can't do action="my_obj::form_submit" as that's not even a valid URI. However, you can do action="/my_obj/form_submit" and map the URI to /<object>/<method> This is the approach that many MVC frameworks use.

webbiedave
Thank you for the suggestion. Unfortunately, this is not done with an MVC.
Tim
Doesn't have to be used in MVC. Was just letting you know that others map URIs to class methods all the time.
webbiedave
that is interesting... I already started with AJAX. I may experiment with this in a future project.
Tim