• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form

#1
[eluser]davy_yg[/eluser]
controllers/ccontactus.php

Code:
<?php

include 'page.php';

class Ccontactus extends Page {

    public function __construct(){

  parent::__construct();

  $this->data['assetscont'] = array(
   'contactcss' => base_url().'assets/css/contact.css'  
  );

    }

    public function index(){
    
  $this->load->view('templates/navigation', $this->data['navigation']);  
  $this->load->view('contactus',$this->data['assetscont']);
  $this->load->view('templates/footer', $this->data['footer']);
    }

public function contact(){

       $this->load->library('form_validation');
    $this->form_validation->set_rules('name', 'name', 'required');
    $this->form_validation->set_rules('email', 'email', 'required');
    $this->form_validation->set_rules('subject', 'subject', 'required');
    $this->form_validation->set_rules('message', 'message', 'required');

    if ($this->form_validation->run() == FALSE)
    {
    $this->load->view('contactus');
    }
    else
    {
    
    
    $this->load->helper(array('form', 'url'));
    $this->load->library('email');
    $this->email->from('[email protected]', 'Your Name');
    $this->email->to('[email protected]');
    $this->email->cc('[email protected]');
    $this->email->bcc('[email protected]');
    $this->email->subject('Email Test');
    $this->email->message('Testing the email class.');
    
    if (!$this->email->send())
    {
    $this->load->view('contactus');
    } else
    {
    $this->load->view('contactus_emailsent');
    }
    
    }
    }

}

?>


routes.php

Code:
$route['contactus'] = 'ccontactus';


views/contactus.php

Code:
<div id="title">Your Message: </div><br><br>

&lt;?php echo form_open('ccontactus/contact', array('id'=>'contactform'));

      echo form_label('Username', 'username');

      $dataName = array(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => '',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

   echo form_input($dataName);

?&gt;

RESULT:

Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\IndonusaCI\application\views\contactus.php on line 72

line 72: &lt;?php echo form_open('ccontactus/contact', array('id'=>'contactform'));


I wonder why?

#2
[eluser]davidMC1982[/eluser]
Because you're only loading the form helper after the form passes validation which is "after the horse has bolted" so to speak. Load the helper in the constructor or, if you'll be using forms most of the time, autoload it.

#3
[eluser]davy_yg[/eluser]

Thanks. At least it shows the form now. Another error after I add this code:

Code:
&lt;?php  
  
   $dataMessage = array(
              'message'     => 'message',
              'id'          => 'message',
              'value'       => '',
              'cols'     => '40',
              'rows'        => '4',
              'style'       => 'width:37%',
     'placeholder' => 'message'
            );

   echo form_textarea($dataMessage);
  
?&gt;<br>


A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: helpers/form_helper.php

Line Number: 264

Why is it?

#4
[eluser]Tpojka[/eluser]
What is page.php?

#5
[eluser]davidMC1982[/eluser]
[quote author="davy_yg" date="1382193408"]
Thanks. At least it shows the form now. Another error after I add this code:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: helpers/form_helper.php

Line Number: 264

Why is it?[/quote]

I'm guessing the form helper requires a name attribute and you didn't define one.

#6
[eluser]CroNiX[/eluser]
In your contact method, you are setting validation rules for a field named "name", but in your form you have the field named "username". So when you submit your form there is no field named "name" which the validation library is expecting.

#7
[eluser]davy_yg[/eluser]

I fixed my view/contactus.php to this:

Code:
<div id="title">Your Message: </div><br><br>

&lt;?php echo form_open('ccontactus/contact', array('id'=>'contactform'));

      $dataName = array(
              'name'        => 'name',
              'id'          => 'name',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'style'       => 'width:32%',
     'placeholder' => 'Your name'
            );

   echo form_input($dataName); ?&gt;<br>
&lt;?php  
  
   $dataEmail = array(
              'email'       => 'email',
              'id'          => 'email',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'style'       => 'width:32%',
     'placeholder' => 'Your email'
            );

   echo form_input($dataEmail); ?&gt;<br>
&lt;?php  
  
   $dataSubject = array(
              'subject'       => 'subject',
              'id'          => 'subject',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'placeholder' => 'Your subject'
            );

   echo form_input($dataSubject);
  
?&gt;<br>
&lt;?php  
  
   $dataMessage = array(
              'message'     => 'message',
              'id'          => 'message',
              'value'       => '',
              'cols'     => '40',
              'rows'        => '4',
              'style'       => 'width:37%',
     'placeholder' => 'message'
            );

   echo form_textarea($dataMessage);
  
?&gt;<br>
&lt;?php    
  
      echo form_submit('submit', 'kirim', array('class' => 'button'));

?&gt;<br>

I autoload my form helper:

autoload.php

Code:
$autoload['helper'] = array('url', 'form');

I still have these two errors:



A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: helpers/form_helper.php

Line Number: 264



A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: helpers/form_helper.php

Line Number: 450

why is it?

#8
[eluser]Tpojka[/eluser]
First error you should solve with adding name parameter to your textarea dataMessage array.
It would be good to add name parameter to every input field.

Second error is indicated because you are using invalid parameters for submit form. As I can see you would like to use form button instead.

#9
[eluser]davidMC1982[/eluser]
As I said, you haven't defined "name" in $dataEmail, $dataSubject or $dataMessage.

eg

Code:
$dataMessage = array(
              'name'     => 'message',
              'id'          => 'message',
              'value'       => '',
              'cols'     => '40',
              'rows'        => '4',
              'style'       => 'width:37%',
              'placeholder' => 'message'
            );


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.