[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><input name="sname" type="text" id="sname" /></td>
</tr>
<tr>
<td align="right">First Name:</td>
<td><input name="fname" type="text" id="fname" /></td>
</tr>
<tr>
<td align="right">Email:</td>
<td><input name="email" type="text" id="email" /></td>
</tr>
<tr>
<td align="right">State:</td>
<td><input name="state" type="text" id="state" /></td>
</tr>
<tr>
<td align="right">Telephone</td>
<td><input name="tel" type="text" id="tel" /></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"> </td>
<td><textarea name="msg" id="msg"></textarea></td>
</tr>
<tr>
<td><input name="date" type="hidden" id="date" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
thanks
[eluser]srpurdy[/eluser]
try
Code: <?php echo form_close();?>
instead of
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)
[eluser]Zed[/eluser]
[quote author="srpurdy" date="1332765101"]try
Code: <?php echo form_close();?>
instead of
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) [/quote]
thanks. i've changed the
Code: </form> to <?php echo form_close();?> and the $this->news_model->set_news(); to $this->services_model->set_advisory();
but still no luck
thanks
[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
[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.
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);
}
[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),
[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
[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');
|