can't POST data from <textarea> in codeigniter - codeigniter

I have been trying to retrieve this data from a textarea using post, but i keep geeting no data back. any help would be appreciated.
Here is my form
<?php echo validation_errors(); ?>
<?php echo form_open('s2e/contactValidation'); ?>
<ul>
<li><label>Subject<input type="text" id="emailsubject" name="subject" placeholder="example#site.com" width="200"></label></li>
<li><label>From<input type="email" id="emailfrom" name="from" ></label></li>
<li><label>Message</label><textarea id="message" name="message" rows="6" cols="200" form="contact" ></textarea></li>
</ul>
<button type"submit" name="sendemail">Send</button>
</form>
My controller looks like this
public function contactValidation(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div id="rerror">', '</div>');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('from', 'From', 'trim|required|valid_email');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
$subject = $this->input->post('subject');
$from = $this->input->post('from');
$message = $this->input->post('message');
if($this->form_validation->run() == FALSE)
{
print_r('failed');
} else {
print_r('good');
}
var_dump($subject);
var_dump($from);
var_dump($message);// this is the culprit that's failing
}
Any help would be appreciated

Why don't you use Codeigniter's Form Helper:
<?php
echo validation_errors();
echo form_open('s2e/contactValidation');
echo form_labe('Subject', 'subject');
echo form_input('subject', 'Value Here', 'id="" placeholder=""');
echo form_label('Email', 'email');
echo form_input('email', '', 'attributes=""');
echo form_textarea('msg', '')
echo form_submit('submit', 'Send Form');
echo form_close();
?>

Related

Can't log in to admin dashboard

My password and username are correct, but I can't login to admin. Is there anything wrong with my controller?
This is my model:
class login_model extends CI_Model{
function cek($username, $password){
$this->db->where("username", $username);
$this->db->where("password", $password);
return $this->db->get("user_admin");
}
function getLoginData($usr, $psw){
$u = $usr;
$p = md5($psw);
$q_cek_login = $this->db->get_where('user_admin', array('username' => $u, 'password' => $p));
if(count($q_cek_login->result()) > 0){
foreach($q_cek_login->result() as $qck){
foreach($q_cek_login->result() as $qad){
$sess_data['logged_in'] = TRUE;
$sess_data['id'] = $qad->id;
$sess_data['username'] = $qad->username;
$sess_data['password'] = $qad->password;
$sess_data['email'] = $qad->email;
$sess_data['level'] = $qad->level;
$this->session->set_userdata($sess_data);
}
redirect('welcome_message');
}
}else{
$this->session->set_flashdata('result_login'. 'username dan password salah');
header('location: '. base_url(). 'login');
}
}
}
This is my controller:
class login extends CI_Controller {
function _construct(){
parent::_construct();
if($this->session->userdata('username')){
redirect(base_url('welcome_message'));
}
$this->load->model(array('login_model'));
}
function index(){
$this->load->view('login');
}
function proses(){
$this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
$this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');
if($this->form_validation->run() == FALSE){
$this->load->view('login');
}else{
$usr = $this->input->post('username');
$psw = $this->input->post('password');
$u = $usr;
$p = md5($psw);
$cek = $this->login_model->cek($u, $p);
if($cek->num_rows() > 0 ){
foreach($cek->result() as $qad){
$sess_data['id'] = $qad->id;
$sess_data['email'] = $qad->email;
$sess_data['username'] = $qad->username;
$sess_data['level']=$qad->level;
$this->session->set_userdata($sess_data);
}
$this->session->set_flashdata('success', 'login berhasil');
redirect(base_url('/'));
}else{
$this->session->set_flashdata('result_login', 'username dan password yang anda masukkan salah');
redirect(base_url('login'));
}
}
}
My view:
<div class="login-box-body">
<p class="login-box-msg">Sign in to start your session</p>
<form action="<?php echo base_url('login/proses'); ?>" method="post">
<?php if (validation_errors() || $this->session->flashdata('result_login')) { ?>
<div class="alert alert-danger animated fadeInDown" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Peringatan!</strong>
<?php echo validation_errors(); ?>
<?php echo $this->session->flashdata('result_login'); ?>
</div>
<?php } ?>
<div class="form-group has-feedback">
<input type="text" class="form-control" placeholder="Username" id="username" name="username">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" class="form-control" placeholder="Password" id="password" name="password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
Register a new admin
</div>
When I try to log in, I always get this error:
I don't see anything strikingly wrong with your code.
Well you are obviously hitting your last nested else which means for some reason this statement $cek->num_rows() > 0 is evaluating to false.
I assume that you are entering a proper and existing username + password combination. But to troubleshoot you can after $p = md5($psw); do:
echo 'hashed password: ' . $p . '<br>username: ' . $u; exit;
and see if the username and hashed password match something in the database, if not, you have your answer. My best guess is that you perhaps didn't store a hashed password and only plain-text one.
As a side note you should move all of your redirects and flashdata out of your model. Models should only return or throw Exceptions.

Ion Auth profile page

I would like a step by step tutorial on how to create a profile page for ion auth codeigniter.
When a logged in user clicks a link User profile link, it opens a user profile page and retrieves all the details of the user in a form so the user can update. I would this to be for the admin users.
Thank you :)
I ended up doing it this way :
below controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library('ion_auth');
}
public function index() {
}
public function login() {
if ($this->input->post()) {
$this->load->library('form_validation');
$this->form_validation->set_rules('identity', 'Identity', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('remember', 'Remember me', 'integer');
if ($this->form_validation->run() === TRUE) {
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember)) {
redirect('dashboard', 'refresh');
} else {
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('admin/user/login', 'refresh');
}
}
}
$data['main_content'] = 'admin/login';
$this->load->view('includes/template', $data);
}
public function logout() {
$this->ion_auth->logout();
redirect('admin/user/login', 'refresh');
}
public function profile() {
$user = $this->ion_auth->user()->row();
//print_r($user);
$this->data['user'] = $user;
//var_dump($user);
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First name', 'trim');
$this->form_validation->set_rules('last_name', 'Last name', 'trim');
$this->form_validation->set_rules('company', 'Company', 'trim');
$this->form_validation->set_rules('phone', 'Phone', 'trim');
if ($this->form_validation->run() === FALSE) {
$this->load->view('admin/edit_profile', $this->data);
} else {
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone')
);
if (strlen($this->input->post('password')) >= 6)
$new_data['password'] = $this->input->post('password');
$this->ion_auth->update($user->id, $data);
redirect('dashboard', 'refresh');
}
//$this->load->view('admin/edit_profile', $data);
}
}
then the view :
<h1><?php echo lang('edit_user_heading');?></h1>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php echo form_input($first_name);?>
</p>
<p>
<?php echo lang('edit_user_lname_label', 'last_name');?> <br />
<?php echo form_input($last_name);?>
</p>
<p>
<?php echo lang('edit_user_company_label', 'company');?> <br />
<?php echo form_input($company);?>
</p>
<p>
<?php echo lang('edit_user_phone_label', 'phone');?> <br />
<?php echo form_input($phone);?>
</p>
<p>
<?php echo lang('edit_user_password_label', 'password');?> <br />
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('edit_user_password_confirm_label', 'password_confirm');?><br />
<?php echo form_input($password_confirm);?>
</p>
<?php if ($this->ion_auth->is_admin()): ?>
<h3><?php echo lang('edit_user_groups_heading');?></h3>
<?php foreach ($groups as $group):?>
<label class="checkbox">
<?php
$gID=$group['id'];
$checked = null;
$item = null;
foreach($currentGroups as $grp) {
if ($gID == $grp->id) {
$checked= ' checked="checked"';
break;
}
}
?>
<input type="checkbox" name="groups[]" value="<?php echo $group['id'];?>"<?php echo $checked;?>>
<?php echo htmlspecialchars($group['name'],ENT_QUOTES,'UTF-8');?>
</label>
<?php endforeach?>
<?php endif ?>
<?php echo form_hidden('id', $user->id);?>
<?php echo form_hidden($csrf); ?>
<p><?php echo form_submit('submit', lang('edit_user_submit_btn'));?></p>

submit multiple inputs within a forloop in codeigniter

My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :
Model is :
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result();
}
My view is:
foreach ($questions as $id => $row) :
?>
<?php
echo "<h5>".$row->question;
echo "<br>";
echo "from : ".$row->user_name."</h5>";
echo date('Y-m-d H:i');
echo "<br>";
$q_no='save'.$row->id;
$ans_no='answer'.$row->id;
echo "<h4> Answer:</h4>";
echo form_open('control_panel');
?>
<textarea name='<?php echo 'answer'.$row->id; ?>' value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea>
<input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' />
<input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' />
<?php
echo form_error($ans_no);
echo "
<div class='form-group'>
<div >
<label class='checkbox-inline'>
<input type='checkbox' name='add_faq' value='yes' />
Adding to FAQ page .
</label>
</div>
</div>
<p>";
?>
<input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/>
<?php
echo 'answer'.$row->id;
?>
<hr>
<?php endforeach; ?>
and my controller is :
$this->load->model('control_panel');
$data['questions']=$this->control_panel->get_questions();
$data['no_of_questions']=count($data['questions']);
if($this->input->post($q_no))
{
$this->form_validation->set_rules($ans_no,'Answer','required|xss_clean');
if($this->form_validation->run())
{
/* code to insert answer in database */
}
}
of course it did not work with me :
i get errors :
Severity: Notice
Message: Undefined variable: q_no
i do not know how to fix it
I am using codeigniter as i said in the headline.
In your controller on your post() you have a variable called q_no you need to set variable that's why not picking it up.
I do not think name="" in input can have php code I think it has to be text only.
Also would be best to add for each in controller and the call it into view.
Please make sure on controller you do some thing like
$q_no = $this->input->post('q_no');
$ans_no = $this->input->post('ans_no');
Below is how I most likely would do lay out
For each Example On Controller
$this->load->model('control_panel');
$data['no_of_questions'] = $this->db->count_all('my_table');
$data['questions'] = array();
$results = $this->control_panel->get_questions();
foreach ($results as $result) {
$data['questions'][] = array(
'question_id' => $result['question_id'],
'q_no' => $result['q_no'],
'ans_no' => $result['ans_no']
);
}
//Then validation
$this->load->library('form_validation');
$this->form_validation->set_rules('q_no', '', 'required');
$this->form_validation->set_rules('ans_no', '', 'required');
if ($this->input->post('q_no')) { // Would Not Do It This Way
if ($this->form_validation->run() == TRUE) {
// Run Database Insert / Update
// Redirect or load same view
} else {
// Run False
$this->load->view('your view', $data);
}
}
Example On View
<?php foreach ($questions as $question) {?>
<input type="text" name="id" value="<?php echo $question['question_id'];?>"/>
<input type="text" name="q_no" value"<?php echo $question['q_no'];?>"/>
<input type="text"name="a_no" value="<?php echo $question['a_no'];?>"/>
<?php }?>
Model
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result_array();
}

CodeIgniter validation and checkbox

i have problem with my checkbox i want to add validation but i don't know how should look my name field in validation rule.
My view
<?php
foreach( $tab1 as $row){
?>
<a>
<input type="checkbox" name="<?php echo 'id_user_'.$row->id_user; ?>" value=" <?php echo $row->id_user; ?>" />
</a>
<?php
When i do this i get result (name of the checkbox )for example:
id_user_1
id_user_2
id_user_3
Now i want to add validation but:
$this->form_validation->set_rules(' WHAT SHOULD I WRITE HERE ??? ', 'User','required');
EDIT:
$this->form_validation->set_rules('id_uzytkownika', 'Uzytkownika','required');
if ($this->form_validation->run())
{
$todo = array(
'tytul_projektu'=>$this->input->post('tytul_projektu'),
'opis_projektu'=>$this->input->post('opis_projektu'),
'data_zakonczenia'=>$this->input->post('datepicker')
);
$user_projekty = array(
'id_uzytkownika'=>$this->input->post('id_uzytkownika'),
'id_projektu'=>$this->input->post('id'),
);
$users = array();
foreach($_POST as $key => $value)
{
if(strpos($key, 'id_uzytkownika') === 0)
{
$users[] = $value;
}
}
$this->Todo_model->add($todo,$user_projekty,$users);
VIEW:
<?php
foreach( $tab1 as $row){
?>
<a>
<input type="checkbox" name="<?php echo 'id_uzytkownika['.$row->id_uzytkownika.']'; ?>" value="<?php echo $row->id_uzytkownika; ?>" />
MODEL:
function add($data,$data2,$users)
{
$this->db->insert('projekty', $data);
$id = $this->db->insert_id('projekty');
$data2['id_projektu'] = $id;
$query = $this->db->query("SELECT * FROM uzytkownicy");
foreach($users as $user) {
$data2['id_uzytkownika'] = $user;
$this->db->insert('projekty_uzytkownicy', $data2); }
return $query->result();
}
There are a few approaches you can take with this. Using your existing code, the first would be to create a loop that defines all unique input names.
foreach($users as $user) {
$this->form_validation->set_rules('id_user_'.$user->id_user, 'User','required');
}
However, this logic would require every checkbox to be checked. Which I am not sure in what circumstance this would be used for.
Another approach would be to change your html a little bit.
Instead of
<input type="checkbox" name="<?php echo 'id_user_'.$row->id_user; ?>"
value="<?php echo $row->id_user; ?>" />
you can try using something like
<input type="checkbox" name="<?php echo 'id_user['.$row->id_user.']'; ?>"
value="1" />
This way you could make a form validation rule based on id_user instead of unique names.

Joomla Component not saving data

I have a component that used to work (Without setting HTML tags to the description) and now after trying to get the HTML formatting to work it won't save.
com_lot\views\lot\tmpl\form.php:
<?php defined('_JEXEC') or die('Restricted access');
$document =& JFactory::getDocument();
$document->addScript('includes/js/joomla.javascript.js');
require_once(JPATH_ADMINISTRATOR .DS. 'components' .DS. 'com_jce' .DS. 'helpers' .DS. 'browser.php');
?>
<form action="index.php" method="post" name="adminForm" id="adminForm">
<script language="javascript" type="text/javascript">
function submitbutton(pressbutton) {
var form = document.adminForm;
if (pressbutton == 'cancel') {
submitform( pressbutton );
return;
}
<?php
$editor =& JFactory::getEditor();
echo $editor->save( 'description' );
?>
submitform(pressbutton);
}
</script>
...
<tr>
<td width="100" align="right" class="key">
<label for="description">
<?php echo JText::_( 'Description' ); ?>:
</label>
</td>
<td>
<?php
$editor =& JFactory::getEditor();
echo $editor->display('description', $this->lotdata->description, '550', '400', '60', '20', false);
?>
</td>
</tr>
...
<input type="hidden" name="option" value="com_lot" />
<input type="hidden" name="lotid" value="<?php echo $this->lotdata->lotid; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="lot" />
<?php echo JHTML::_( 'form.token' ); ?>
<button type="button" onclick="submitbutton('save')"><?php echo JText::_('Save') ?></button>
<button type="button" onclick="submitbutton('cancel')"><?php echo JText::_('Cancel') ?></button>
</form>
com_lot\models\lot.php:
function store($data)
{
// get the table
$row =& $this->getTable();
// Bind the form fields to the hello table
if (!$row->bind($data)) {
$this->setError($this->_db->getErrorMsg());
return false;
}
// Make sure the hello record is valid
if (!$row->check()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
// Store the web link table to the database
if (!$row->store()) {
$this->setError( $row->getErrorMsg() );
return false;
}
return true;
}
function save()
{
// Check for request forgeries
JRequest::checkToken() or jexit( 'Invalid Token' );
// get the model
$model =& $this->getModel();
//get data from request
$post = JRequest::get('post');
$post['description'] = JRequest::getVar('description', '', 'post', 'string', JREQUEST_ALLOWRAW);
// let the model save it
if ($model->store($post)) {
$message = JText::_('Success');
} else {
$message = JText::_('Error while saving');
$message .= ' ['.$model->getError().'] ';
}
$this->setRedirect('index.php?option=com_lot', $message);
}
Any help appreciated.
Edit: I have seen stuff about JForms and having XML files... is this applicable? I haven't found anywhere that says what they're used for, just what types there are...
I found the problem (once I'd cleaned up the code a bit) was that in the article I was following (http://docs.joomla.org/How_to_use_the_editor_in_a_component) missed changing store() to store($data).
Because the pages redirect etc it doesn't die and error out. Thanks to for Jan for your help.

Resources