Speed up development with full-stack environments for every branch.

Learn More

Working with forms in Yii [PHP]

1913 Runs 10150 Views 2791 Copies
Saved

Saved

cezary 46

cezary
published 4 years ago

<?php

// This is the main controller for the application
// A controller is a class that handles all the logic
class SiteController extends CController
{
	// Define an action. actionIndex is a default action
	public function actionIndex()
	{
		// Load a model and a form
		$model = new Contact;
		$form = new CForm('application.views.site.contactForm', $model);
	
		// If form is submitted and data is correct...
		if($form->submitted('send') && $form->validate())
		{
			// Save data and redirect to show what has been entered
			$model -> save();
			$this -> redirect(array('site/list'));
		}
		else
			// else, show the form again
			$this -> render('contact', array('form' => $form));
	}
	
	public function actionList()
	{
		$this -> render('list', array('data' => Contact::model()->findAll()));
	}
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).