Can I run object method directly from form action=""

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
codeCutterFL
Forum Newbie
Posts: 2
Joined: Thu Nov 04, 2010 10:51 am

Can I run object method directly from form action=""

Post by codeCutterFL »

Hi All,

I am a brand new member.

I am trying to keep all of my code contained within an object (class). I am working on a User Registration module. I have a form that is displayed using:

$register->display_registration_form();

I am wondering if, instead of using:

<form action="register.php" method="post">

can I somehow run a method (function) in the current object upon form submission (by using action="")?

I suppose I could do the following in register.php:

$register->process_form( ... );

but, curious if could eliminate that extra .php program.

Thanks in advance.

Jim
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can I run object method directly from form action=""

Post by AbraCadaver »

You can use action="register.php?action=process_form" or just action="register.php" and a hidden input with the name of "action" and value "process_form". Then on register.php or in some other file that is loaded before, you can code some logic to direct it to the proper method.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can I run object method directly from form action=""

Post by requinix »

To answer the question as it was asked: no, you can't. You need some bit of PHP code somewhere.

If you really want this OOP approach then (a) learn a bit about MVC and (b) consider writing a little helper function or something that'll "dispatch" actions to functions/object methods as needed.
However (b) could easily be more work than you need so just go with Woody's advice anyways.
codeCutterFL
Forum Newbie
Posts: 2
Joined: Thu Nov 04, 2010 10:51 am

Re: Can I run object method directly from form action=""

Post by codeCutterFL »

Thanks for your responses!

Currently I have a register page object which contains my form and some methods for processing the form. When I submit the form I call a program (action="member.php") which performs validation and attempts to register the new user (this is my non-JavaScript version, BTW). If there is an error, I am redirecting back to the "calling" program (page) - using header("Location: register.php"). I am using a session variable to store my register object so that the values of my form fields are accessible from registerpage.php and member.php:

$register = new RegisterPage();
$_SESSION['REGISTERPAGE'] = $register;
...
$_SESSION['REGISTERPAGE']->register( ...);

I am wondering now if this is a good way to do it.

Are you saying that registerpage.php should represent my "View", member.php should be my "Controller" and that I should to add another object (or .php program) to represent my "Model"?

Or would you say that the way that I am doing it now is okay?

Thanks in advance.

Jim
Post Reply