Reputation: 31
I am working on a small project in which users create accounts in it and then they can add projects, edit projects, see projects. When user adds projects his/her id (available in the session) is also added into the project table. I want to add projects using dynamic codeigniter form. When the create_project_view is loaded and the user is logged in the form is visible on the page, but when I click for its source code on the source code the form is not visible, however if user logs out the form is visible on screen as well as on the source code. I don't know why the form is not visible on the source code. and this could be the possible reason the I am not able to add data as there is no form though shown on the page. and this could also be the reason that when I click on the submit button and I am logged in, it logs me out.
I was using PHP version:7.2.1, but then downgraded to PHP version:5.6.3 still I have that problems. Could you please help me in this issue?
Below I am sharing my code and the screenshots in both condition when user is logged in and logged out
screenshot when user is logged in:
Here is a button code which takes me to the (create) controller
<h1>Projects</h1>
<table class="table table-hover">
<thead class="bg-warning">
<a class="btn btn-primary pull-right" href="<?php echo base_url();?>projects/create">Create Project</a><br>
<tr>
<th>Project Name</th>
<th>Project Created</th>
</tr>
</thead>
<tbody>
<?php foreach($projects as $project): ?>
<tr>
<?php echo"<td><a href='".base_url()."projects/display/".$project->id."'>".$project->project_name."</a></td>" ?>
<?php echo"<td>".$project->created_date."</td>" ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
2-(Controller) projects.php : which contains the 'create' project function
class Projects extends CI_Controller
{
public function create()
{
$this->form_validation->set_rules('project_name','Project
Name','trim|required');
$this->form_validation->set_rules('project_body','Project
Body','trim|required');
if($this->form_validation->run()==false)
{
$data['main_view']="projects/create_project_view";
$this->load->view('Layouts/main',$data);
}
else
{
$user_data= array(
'user_project_id' =>$this->session->userdata('user_id'),
'project_name' =>$this->input->post('project_name'),
'project_body' =>$this->input->post('project_body'));
if($this->project_model->create_project($user_data))
{redirect("projects/index");}
}
}
}
?>
3- (Modal) porject_model.php
<?php
class project_model extends CI_Model
{
public function create_project($data)
{
$insert_query=$this->db->insert('projects', $data);
return $insert_query;
}
}
?>
4. (View): Create_project_view.php: Through this form I send data to "projects" controller
<h1>Registration Form</h1>
<?php $attributes=array('Id'=>'reg_form','class'=>'form-vertical');?>
<?php echo form_open('projects/create',$attributes);?>
<div class="form-group">
<?php echo form_label('Project title:'); ?>
<?php $data=array(
'Name'=>'project_name',
'class'=>'form-control',
'placeholder'=>'Enter Your First Name:');?>
<?php echo form_input($data); ?>
</div>
<div class="form-group">
<?php echo form_label('project body:'); ?>
<?php $data=array(
'Name' =>'project_body',
'class' =>'form-control',
'placeholder' =>'Enter Your User Name:');?>
<?php echo form_textarea($data); ?>
</div>
<div class="form-group">
<?php $data=array(
'Name'=>'reg_button',
'class'=>'btn btn-primary',
'value'=>'Register');?>
<?php echo form_submit($data); ?>
</div>
<?php echo form_close(); ?>
Upvotes: 1
Views: 152
Reputation: 31
Jamshid Hashimi's answer was correct. I am just sharing the code here so others may also benefit and don't get into similar trap as I got into. Actually the problem was in other view called (login_view) which I had not post it. I had two forms in one page. I opened the first page and then without closing the first form I opened the second form. On line 10th I commented the line I forgot.
<!--When user is logged in -->
<?php if($this->session->userdata('loged_in')): ?>
<h2>Logout</h2>
<?php echo"You are loged in as " . $this->session->userdata('user_name');?>
<?php echo form_open('users/logout');?>
<?php $data=array(
'name'=>'lgout',
'class'=>'btn btn-primary',
'value'=>'Log out');?>
<?php echo form_submit($data); ?>
<?php echo form_close(); ?> **<!--I forgot this line -->**
<?php else: ?>
<!--When user is not logged in -->
<h2>login</h2>
<?php $attributes=array('id'=>'regform','class'=>'form-horizontal'); ?>
<?php if($this->session->flashdata('errors')): ?>
<?php echo $this->session->flashdata('errors'); ?>
<?php endif; ?>
<!--form starts here-->
<?php echo form_open('users/login',$attributes); ?>
<div class="form-group">
<?php echo form_label('User Name:');?>
<?php $data=array(
'name'=>'username',
'class'=>'form-control',
'placeholder'=>'Enter Your Name'); ?>
<?php echo form_input($data); ?>
</div>
<div class="form-group">
<?php echo form_label('User Password:');?>
<?php $data=array(
'name'=>'userpass',
'class'=>'form-control',
'placeholder'=>'Enter Your Name'); ?>
<?php echo form_password($data); ?>
</div>
<div class="form-group">
<?php $data=array(
'name'=>'sbmt',
'class'=>'btn btn-primary',
'value'=>'Login'); ?>
<?php echo form_submit($data); ?>
</div>
<?php echo form_close(); ?> <!--form ends here-->
Upvotes: 0
Reputation: 7823
You may have two forms in the same page that are conflicting. If you have your logout function inside a form (which is not necessary, you can make it a direct link by giving onClick event to your button), your "Submit" action of creating a project may trigger that action, instead of its own. It looks that's why you are logging out.
Otherwise, I can see no other issues in your forms. As you develop more applications, you will adopt a better structure and that way, you won't get into traps like this.
Upvotes: 1