Contact Form action

In default YII program generator, contact form generated by YII. So I want to change when submit button click save data to database not send to email.

I created table to saving information of contact, hire the table information:

T_msg: id, name, title,email, subject, content

I get function to processing data when submit button clicked.




$model=new ContactForm;

		if(isset($_POST['ContactForm']))

		{

			$model->attributes=$_POST['ContactForm'];

			if($model->validate())

			{

				//$headers="From: {$model->email}\r\nReply-To: {$model->email}";

//				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);

				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

				$this->refresh();

			}

		}

		$this->render('contact',array('model'=>$model));

	}



I’m still confused, how can sent to the Site Controller controller, when user enter the data from contact form?

Thanks you

Hi Hermans,

Do you mean, how can the SiteController knows what data were entered by the user in the contact form?

It happens in this line:


$model->attributes=$_POST['ContactForm'];



Take a look at the page source of the Contact page, you should see the name of the fields there like ContactForm[name],ContactForm[email], etc. Simply put, $model->name will be equal to the value of ContactForm[name], and so on. Then it is being validated, and sends the email if validation passed.

thank you, very helpful

what SiteController should be there in Yii ? I see that the login and contact in working on this controller

Hi Hermans,

I’m not sure if I got your question correctly…would you mind rephrasing your question? I just would like to give you accurate answer. :)

Previously I thought that SiteController it should be in every website built with Yii.

however, after I know about the search for Site Controller is the controller name.

For example:

http://localhost:81/prj/index.php?r=site/contact

the site Controller is SiteController

and contact is Action in SiteController ->actionContact




$this->render('contact',array('model'=>$model));



above code, call contact view at ‘views->site->contact.php’

how if ‘contact.php’ is placed in ‘views->contact->contact.php’ ??

Then you should render page like this:




$this->render('/contact/contact',array('model'=>$model));



I haven’t tested this code, inform me if this doesn’t work. As you might have noticed, the first parameter calls the appropriate ‘view’ file, and what we did was just to point it to our desired ‘view’ file (contact.php), which is located under /views/contact/

thank you :)

No problem :)