Page 1 of 1
Can I run object method directly from form action=""
Posted: Thu Nov 04, 2010 11:11 am
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
Re: Can I run object method directly from form action=""
Posted: Thu Nov 04, 2010 11:17 am
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.
Re: Can I run object method directly from form action=""
Posted: Thu Nov 04, 2010 12:32 pm
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.
Re: Can I run object method directly from form action=""
Posted: Thu Nov 04, 2010 5:56 pm
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