Welcome Guest, Not a member yet? Register   Sign In
Form not working
#1

[eluser]Zed[/eluser]
Hi,
i don't really know why my form isn't working, even when i click on empty, its still not validating.
Here's my code:

Controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Services extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('services_model');
}


public function view($page = 'easylife')
{

  if ( ! file_exists('application/views/services/'.$page.'.php'))
{
  // Whoops, we don't have a page for that!
  show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('services/'.$page, $data);
$this->load->view('templates/footer', $data);

}
public function advisory()

{
$this->load->helper('form');
$this->load->library('form_validation');

//$data['title'] = 'Create a news item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('sname', 'Surname', 'required');
$this->form_validation->set_rules('fname', 'First Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('state', 'State', 'required');
$this->form_validation->set_rules('tel', 'telephone', 'required');
$this->form_validation->set_rules('msg', 'Message', 'required');

if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('services/'.$page, $data);
$this->load->view('templates/footer', $data);
  
}
else
{
  $this->news_model->set_news();
  $this->load->view('services/success');
}
}
}

Model

Code:
<?php

class Services_model extends CI_Model {

public function __construct()
{
  $this->load->database();
}
     function insert_entry()
    {
        $data = array(
               'title' => $title,
               'sname' => $name,
      'fname' => $name,
      'email' => $name,
      'state' => $name,
      'tel' => $name,
      'msg' => $name,
               'date' => $date
            );

  $this->db->insert('advisory', $data);
  
  
}
  

    }

View

Code:
<?php echo validation_errors(); ?>
              <?php echo form_open('services/advisory'); ?>

                <table width="80%" border="0" cellspacing="2" cellpadding="0">
                  <tr>
                    <td align="right"> Title:</td>
                    <td><select name="title" id="title">
                      <option value="mr">Mr</option>
                      <option value="mrs">Mrs</option>
                      <option value="ms">Ms</option>
                    </select></td>
                  </tr>
                  <tr>
                    <td align="right">Surname:</td>
                    <td>&lt;input name="sname" type="text" id="sname" /&gt;&lt;/td>
                  </tr>
                  <tr>
                    <td align="right">First Name:</td>
                    <td>&lt;input name="fname" type="text" id="fname" /&gt;&lt;/td>
                  </tr>
                  <tr>
                    <td align="right">Email:</td>
                    <td>&lt;input name="email" type="text" id="email" /&gt;&lt;/td>
                  </tr>
                  <tr>
                    <td align="right">State:</td>
                    <td>&lt;input name="state" type="text" id="state" /&gt;&lt;/td>
                  </tr>
                  <tr>
                    <td align="right">Telephone</td>
                    <td>&lt;input name="tel" type="text" id="tel" /&gt;&lt;/td>
                  </tr>
                  <tr>
                    <td align="right">Topic:</td>
                    <td><select name="topic" id="topic">
                      <option value="0" selected="selected">Select Topic</option>
                      <option value="1">PAYE</option>
                      <option value="2">Capital Restructuring</option>
                    </select></td>
                  </tr>
                  <tr>
                    <td rowspan="2">&nbsp;</td>
                    <td>&lt;textarea name="msg" id="msg"&gt;&lt;/textarea></td>
                  </tr>
                  <tr>
                    <td>&lt;input name="date" type="hidden" id="date" /&gt;&lt;/td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>&lt;input type="submit" name="Submit" value="Submit" /&gt;&lt;/td>
                  </tr>
                  <tr>
                    <td colspan="2">&nbsp;</td>
                  </tr>
                </table>
              &lt;/form&gt;

thanks
#2

[eluser]srpurdy[/eluser]
try

Code:
&lt;?php echo form_close();?&gt;

instead of

Code:
&lt;/form&gt;

Also
Code:
$this->news_model->set_news();

Where is the set_news() function. confirm that it exists, because the function you posted is from the services model which your not calling.


Also you may want to read up on
<fieldset> and <label> and avoid using html tables. (yuck) Smile
#3

[eluser]Zed[/eluser]
[quote author="srpurdy" date="1332765101"]try

Code:
&lt;?php echo form_close();?&gt;

instead of

Code:
&lt;/form&gt;

Also
Code:
$this->news_model->set_news();

Where is the set_news() function. confirm that it exists, because the function you posted is from the services model which your not calling.


Also you may want to read up on
<fieldset> and <label> and avoid using html tables. (yuck) Smile[/quote]

thanks. i've changed the
Code:
&lt;/form&gt; to &lt;?php echo form_close();?&gt; and the $this->news_model->set_news(); to $this->services_model->set_advisory();
but still no luck
thanks
#4

[eluser]CodeIgniteMe[/eluser]
Did you define the set_advisory() method in the Services_model Class? I assume that one of the problems is your $page variable on
Code:
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('services/'.$page, $data);
$this->load->view('templates/footer', $data);
  
}
because you never declared and/or initialized the variable $page before you used it. $page is empty
#5

[eluser]srpurdy[/eluser]
[quote author="CodeIgniteMe" date="1332830837"]Did you define the set_advisory() method in the Services_model Class? I assume that one of the problems is your $page variable on
Code:
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('services/'.$page, $data);
$this->load->view('templates/footer', $data);
  
}
because you never declared and/or initialized the variable $page before you used it. $page is empty[/quote]

Good catch missed that.

@OP
Your services model you have is showing this function
function insert_entry()

yet your calling another function on a successful submit. So It won't work unless you call a function that exists.

From what I gather
This...
Code:
$this->services_model->set_advisory();

Should be
Code:
$this->services_model->insert_entry();

Unless we're missing part of the code of course. Smile

Also besides this. your not passing anything to the insert_entry function. So the array data will actually be blank when that function finally gets triggered.

You should replace the variables with $this->input->post('sname') and so on.

You can also do this in the controller first and than pass them to the insert function.

So for example.
Code:
$title = $this->input->post('title');
$sname = $this->input->post('sname');
$fname = $this->input->post('fname');
$email = $this->input->post('email');
$state = $this->input->post('state');
$telephone = $this->input->post('tel');
$message = $this->input->post('msg');
$date_post = $this->input->post('date');

And also include those as variables passing to the insert function
Code:
$this->services_model->insert_entry($title,$sname,$fname,$email,$state,$telephone,$message,$date_post);

Than that function should change to

Code:
function insert_entry($title,$sname,$fname,$email,$state,$telephone,$message,$date_post)
    {
    $data = array(
      'title' => $title,
      'sname' => $sname,
      'fname' => $fname,
      'email' => $email,
      'state' => $state,
      'tel' => $telphone,
      'msg' => $message,
      'date' => $date_post
      );
  $this->db->insert('advisory', $data);
  
}


You can also do it this way which is a bit more simple way to do it. Which way is better depends on how you will use the data. In some cases you may re-use the same data somewhere else so creating variables like above is useful in those cases.

Code:
function insert_entry()
    {
    $data = array(
      'title' => $this->input->post('title'),
      'sname' => $this->input->post('sname'),
      'fname' => $this->input->post('fname'),
      'email' => $this->input->post('email'),
      'state' => $this->input->post('state'),
      'tel' => $this->input->post('tel'),
      'msg' => $this->input->post('msg'),
      'date' => $this->input->post('date')
      );
  $this->db->insert('advisory', $data);
  
}
#6

[eluser]InsiteFX[/eluser]
If your going to use:
Code:
'title' => $this->input->post('title'),

// all inputs should have the second parameter for xss_clean!
'title' => $this->input->post('title', TRUE),
#7

[eluser]CodeIgniteMe[/eluser]
I noticed you have this method in your Controller
Code:
public function view($page = 'easylife')
{

  if ( ! file_exists('application/views/services/'.$page.'.php'))
{
  // Whoops, we don't have a page for that!
  show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('services/'.$page, $data);
$this->load->view('templates/footer', $data);

}

You might consider rewriting this as
Code:
public function view($page='easylife', $data=array(),$return=false)
{

  if ( ! file_exists('application/views/services/'.$page.'.php'))
{
  // Whoops, we don't have a page for that!
  show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$view = $this->load->view('templates/header', $data, true);
$view .= $this->load->view('services/'.$page, $data, true);
$view .= $this->load->view('templates/footer', $data, true);

if($return) return $view;
else echo $view;

}

So instead of redundancy instances like this
Code:
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('services/'.$page, $data);
$this->load->view('templates/footer', $data);
  
}
else
{
  $this->news_model->set_news();
  $this->load->view('services/success');
}

You can simply use (and reuse) your method
Code:
if ($this->form_validation->run() === FALSE)
{
$this->view('services/'.$page,$data);
}
else{
  $this->news_model->set_news();
  $this->load->view('services/success');
}

A better approach is to place this in a model so you can just load it from any controller
#8

[eluser]srpurdy[/eluser]
[quote author="InsiteFX" date="1332849601"]If your going to use:
Code:
'title' => $this->input->post('title'),

// all inputs should have the second parameter for xss_clean!
'title' => $this->input->post('title', TRUE),
[/quote]

Or you can put it in the form validation.

Code:
$this->form_validation->set_rules('title', 'Title', 'required|xss_clean');




Theme © iAndrew 2016 - Forum software by © MyBB