Welcome Guest, Not a member yet? Register   Sign In
cannot send hidden fields to the controller
#1

[eluser]dinisptc[/eluser]
cannot send hidden fields to the controller

Code:
else
   {
   //$this->load->view('formsuccess');
   // get the user object      
   $data->the_user = $this->ion_auth->user()->row();
              // put the user object in class wide property--->---->-----
              $this->the_user = $data->the_user;
            
              // load $the_user in all displayed views automatically
              $this->load->vars($data);


   //set the flash data error message if there is one
   $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  
   $DContent['page_details'] = array('page_title' => 'Index of freejoBoard');
          $Dheader = array();
          $Dsidebar = array();
          $Dfooter = array();

   $today = date("j, n, Y");
                        if ($emprego_id = $this->empregos_model->add($_POST))
                        {

                        }



          //preenche as variaveis
          $this->template->write_view('header', 'html/header', $Dheader, true);
          $this->template->write_view('content', 'empregos/emprego_sucesso',$this->data,true);
          $this->template->write_view('sidebar', 'html/sidebar');
          $this->template->write_view('footer', 'html/footer');
          $this->template->render();

   }


the above id the controller

the view its below
Code:
<?php echo validation_errors(); ?>

<?php echo form_open('empregos/inserir'); ?>

<?php

$today = date("j, n, Y");


form_hidden('data_criacao', $today);

form_hidden('identidade', $this->session->userdata('user_id'));
?>
<!--<input type="hidden" name="data_criacao" value=<?php $today ?> />
<input type="hidden" name="identidade" value=<?php $this->session->userdata('user_id') ?> />-->
<?php
echo br(2);

/* input text titulo */
$data = array(
    'name' => 'titulo',
    'id' => 'titulo',
    'value' => set_value('titulo'),
    'maxlength' => '500',
    'size' => '500'
);



echo form_label('Job Title', 'titulo');
echo br(1);
echo form_input($data);

echo br(2);


/* input text descricao */
$data = array(
    'name' => 'descricao',
    'id' => 'descricao',
    'value' => set_value('descricao'),
    'maxlength' => '5000',
    'size' => '5000'
);



echo form_label('Job Description', 'descricao');
echo br(1);
echo form_input($data);

echo br(2);

/* input text referencia */
$data = array(
    'name' => 'referencia',
    'id' => 'referencia',
    'value' => set_value('referencia'),
    'maxlength' => '200',
    'size' => '200'
);



echo form_label('Reference', 'referencia');
echo br(1);
echo form_input($data);

echo br(2);

/* input text nome_empresa */
$data = array(
    'name' => 'nome_empresa',
    'id' => 'nome_empresa',
    'value' => set_value('nome_empresa'),
    'maxlength' => '200',
    'size' => '200'
);



echo form_label('Company Name', 'nome_empresa');
echo br(1);
echo form_input($data);

echo br(2);


/* input text local_de_trabalho */
$data = array(
    'name' => 'local_de_trabalho',
    'id' => 'local_de_trabalho',
    'value' => set_value('local_de_trabalho'),
    'maxlength' => '200',
    'size' => '200'
);



echo form_label('Workplace', 'local_de_trabalho');
echo br(1);
echo form_input($data);

echo br(2);

/* input text email */
$data = array(
    'name' => 'email',
    'id' => 'email',
    'value' => set_value('email'),
    'maxlength' => '200',
    'size' => '200'
);



echo form_label('Email', 'email');
echo br(1);
echo form_input($data);

echo br(2);
?>
      
<p>&lt;?php echo form_submit('submit', 'Insert New Job');?&gt;</p>

<p><a href="&lt;?= site_url('empregos/listar') ?&gt;">Cancel</a></p>
      
&lt;?php echo form_close();?&gt;

the result its an error

the value of identidade and data_criacao its always null

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: identidade

Filename: models/empregos_model.php

Line Number: 30
A PHP Error was encountered

Severity: Notice

Message: Undefined index: data_criacao

Filename: models/empregos_model.php

Line Number: 34
A Database Error Occurred

Error Number: 1048

Column 'identidade' cannot be null

INSERT INTO `empregos` (`identidade`, `titulo`, `descricao`, `referencia`, `data_criacao`, `nome_empresa`, `email`, `local_de_trabalho`) VALUES (NULL, 'sdfesrew', 'cxvxcv', 'ccccc', NULL, 'eee', '[email protected]', 'cccccc')

Filename: /var/www/freejoboard/models/empregos_model.php

Line Number: 39
#2

[eluser]marcogmonteiro[/eluser]
Have you tried sending them as non hidden files and see how it goes?
(just to check if the values)

When you do print_r($this->input->post()) are the values there or not?

Also you should use $_POST to pass the data to your model always use the input library in Codeigniter.

If none of that works, show us your model. (I bet the problem is there)
#3

[eluser]dinisptc[/eluser]
Code:
Array ( [titulo] => asddqwfqw [descricao] => cccccccccc [referencia] => eee [nome_empresa] => cccccc [local_de_trabalho] => dghdgh [email] => [email protected] [submit] => Insert New Job )



i did print_r($this->input->post()); and the values are not there

i am going to show the model
but i think the problem is in the form

Code:
function add($data) {
        $result = false;
    $new_data=array(
     'identidade'=>$data['identidade'],
     'titulo'=>$data['titulo'],
     'descricao'=>$data['descricao'],
     'referencia'=>$data['referencia'],
     'data_criacao'=>$data['data_criacao'],
     'nome_empresa'=>$data['nome_empresa'],
     'email'=>$data['email'],
     'local_de_trabalho'=>$data['local_de_trabalho']);
        $this->db->set($new_data);
        $this->db->insert('empregos');

        if ($this->db->affected_rows() > 0) {
            $result = $this->db->insert_id();
        }
        return $result;
    }
#4

[eluser]dinisptc[/eluser]
solved this issue
Code:
&lt;?php echo validation_errors();

$today =date("Y-m-d");
$hidden = array('data_criacao' => $today, 'identidade' => $this->session->userdata('user_id'));

?&gt;
&lt;?php echo form_open('empregos/inserir','', $hidden); ?&gt;

thanks
Obrigado
Pedro
#5

[eluser]boltsabre[/eluser]
On a side note, why you passing stuff like

Code:
$today =date("Y-m-d");
$hidden = array('data_criacao' => $today, 'identidade' => $this->session->userdata('user_id'));

from your view, to your controller, and then finally to your model?

- You exposed the "user_id" in a public html form (it MAY be a security problem),
- You've created extra coding for yourself (thus potential for extra bugs).
- You've made extra variables (thus making your server work harder)
- You're html code is now bigger (increasing the size of the file that needs to be served to the user, eating up your bandwidth, etc).

You could have easily just done this in your model!
Code:
$new_data=array(
     'identidade'=>$this->session->userdata('user_id'),
     'titulo'=>$data['titulo'],
     'descricao'=>$data['descricao'],
     'referencia'=>$data['referencia'],
     'data_criacao'=>date("Y-m-d"),
     'nome_empresa'=>$data['nome_empresa'],
     'email'=>$data['email'],
     'local_de_trabalho'=>$data['local_de_trabalho']);

Don't feel bad, I think we all start coding like this, but this is a much better way of coding, you should only ever assign something to a variable if you absolutely have to! Glad you go your problem solved!




Theme © iAndrew 2016 - Forum software by © MyBB