theAndDev
theAndDev

Reputation: 672

How to submit form name as object to a php function

I am trying to learn mvc framework in php but then i could not find out a way in passing the form data to a php function in another php page. I am submitting the form and receiving it in the same page and then I am trying to pass the form data to the controller function which will handle the validation. I cannot figure out how should I pass the form data to the other function.

I can pass each data as parameter but then that would be lengthy if there are lots of data in the form. I was wondering if I could pass the form as an object (something like we pass object of structures) to another function and then use the data suitably. I have put in a code module below:

<?php
include('controller.php');
$controller = new Controller($model);
if (isset($_GET['formButton'])) 
$controller->submitButtonClicked();
?>
<form name="details" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="txt1">First Name:</label>
<input type="text" name="txt1" id="txt1"/>
<label for="password">Password:</label>
<input type="password" name="password" id="password"/>
<input type="submit" name="formButton" id="formButton" value="Submit"/>
</form>

Any help would be very helpful.

Upvotes: 2

Views: 1899

Answers (3)

Aitazaz Khan
Aitazaz Khan

Reputation: 1605

If you really want to work with MVC model view controller than i suggest you the codeigniter everything is well defined easy to use visit the official site of codeigniter. http://ellislab.com/codeigniter. I hope you will like to work with codeigniter try it and get rid of alot of coding.

Upvotes: 2

algoni
algoni

Reputation: 635

The $_GET and $_POST superglobals are already arrays of the submitted form data, so you can simply use these in your controller. Just make the form submit to the controller file directly: this is cleaner and there's no need to pass the $_GET or $_POST (you should probably use post, but I don't know the context).

I assume you're building your own MVC from scratch. If so, you could do a handler.php controller, that every form submits to. This could loop the posted data like so:

// define Input class somewhere and include
$input = new Input();
foreach($_POST as $field => $value)
{
  $input->$field = $this->validate($value);
}

In validate() you would do some general validation. Then you could use this new Input object wherever you need the input data. This is a very primitive example of how premade frameworks like CodeIgniter and Laravel use an Input helper class, and of course you can expand on this. Or better yet, save some extra work and utilize a good known framework like those mentioned in your project :)

Upvotes: 1

Andreas Saarva
Andreas Saarva

Reputation: 73

First of all, why are you using $_GET?

Second, the $_GET is a global, which should be available in any class used by your system (doesn't matter if it's in another file or not).

Otherwise, you can simply just pass them on to the method of the class, if you want to do that for some reason:

...

$controller->submitButtonClicked($_GET);

...

Upvotes: 0

Related Questions