click submit button but cannot found page - codeigniter

I want to make calculate the area of square, but when I click the submit button, page cannot be found :
controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Controller_luas extends CI_Controller {
public function coba_controller($panjang=0,$lebar=0){
$nilai['panjang_bangunan'] = $panjang;
$nilai['lebar_bangunan'] = $lebar;
$nilai['luas_bangunan'] = $this->mymodel->hitungluas($nilai);
$this->load->view('coba_view',$nilai);
}
}
view :
<form method="POST" action="<?php echo base_url()."index.php/controller_luas/"; ?>">
<table widht="200" border="1">
<tr>
<td>Panjang</td>
<td><input type="text" name="cari_judul"></td>
</tr>
<tr>
<td>Lebar</td>
<td><input type="text" name="cari_penulis"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Hasil"></td>
</tr>
</table>
<h1>Luas Perseginya yaitu : <?php echo $luas_bangunan ?><h1>
</form>
models :
function hitungluas($param=''){
$luas=$param['panjang_bangunan'] * $param['lebar_bangunan'];
return $luas;
}

When you are submitting to the controller Controller_luas
It is submitting to the index function even though I do not see it there.
If you need it to go to coba_controller function then in form action you need to add it.
With index.php
<form method="POST" action="<?php echo base_url('index.php/controller_luas/coba_controller'); ?>">
Without index.php
<form method="POST" action="<?php echo base_url('controller_luas/coba_controller'); ?>">
Using Codeigniter Form Helper
http://www.codeigniter.com/user_guide/helpers/form_helper.html
Or you can auto load this
$this->load->helper('form');
<?php echo form_open('controller_luas/coba_controller');?>
// Form Content
<?php echo form_close();?>
You also might be best to use codeigniter form validation library as well.

Related

Delete Data using code igniter not working

I'm trying to delete data by id but it doesn't work.
I have some code like this.
Controller :
public function delete()
{
if (isset($_GET['del'])) {
$ii = $this->input->post('id');
$this->P6_model->delete($ii);
$this->load->view('p6home');
}
}
Model :
public function delete($i)
{
return $this->db->delete('contacts', array('id' => $i));
}
Routes :
$route['deldata'] = 'p6/delete';
View :
foreach ($contacts as $data) {
?>
<tr>
<input type="hidden" name="id" value="<?=$data->id?>"/>
<td data-label="Name"><?=$data->name?></td>
<td data-label="address"><?=$data->address?></td>
<td data-label="phone"><?=$data->phone?></td>
<td>
<button class="positive ui button">Update</button>
<button class="negative ui button" onclick="return confirm('Are you sure?')" name="del" href="deldata">Delete</button>
</td>
</tr>
<?php
}
?>
Thanks before.
you must have declared base_url, right? Use that in your link to redirect a page. Also, give the get parameter in the URL itself. POST will not work without a form. This should work for you. View:
<?php
foreach ($contacts as $data) {
?>
<tr>
<td data-label="Name"><?=$data->name?></td>
<td data-label="address"><?=$data->address?></td>
<td data-label="phone"><?=$data->phone?></td>
<td>
<button class="positive ui button">Update</button>
<button class="negative ui button" onclick="return confirm('Are you sure?')" name="del" href="<?php echo base_url('deldata')."/$data->id"; ?>">Delete</button>
</td>
</tr>
<?php
}
?>
Controller:
public function delete($id)
{
$this->P6_model->delete($id);
$this->load->view('p6home'); //instead of loading the view here, try redirecting to a controller function and load the view there.
}

How do I pass values from views to controller

In a view, When we route a button to a function inside the controller, how can we pass two or more values from present during that view.
I was practicing creating a result management system of students. In the view routed from index of ResultController, we have link options to view mark sheet of class ..or individual student. When we click on select class, it redirects to a view where there is two dropdowns to choose the class and batch of students. When we choose respected class and batch, the values class_id and batch_id is routed to function result inside ResultControler, we select students from that class and batch.. and respected subjects and return a view. In that view, we show the marksheet of students(if theres one), and below I have included a button to add marks/create marksheet.
But, I am so confused how I can pass those class_id and batch_id to create function inside ResultController, from the button.
public function index()
{
return view('resultmainpage');
}
public function choose()
{
$classes= Sclass::all();
$batches= Batch::all();
return view('chooseclassbatchresult',compact('classes','batches'));
}
public function result(Request $request)
{
$classid = $request->class;
$batchid = $request->batch;
//dd($batchid);
$students =Student::where('sclass_id',$classid)
->where('batch_id', $batchid)
->whereHas('subject')
->get();
$class= Sclass::findOrFail($classid);
return view('showstudentresult',compact('students','class','classid','batchid'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
// I need class_id and batch_id here
// dd($classidd);
$students = Student::where('sclass_id',$classid)
->where('batch_id',$batchid)
->whereDoesntHave('subject')
->get();
//dd($students);
Route:
Route::get('/rms','MainPageController#index')->name('rms');
Route::get('results/choose','ResultController#choose')->name('chooseresult');
Route::post('/showstudentresult','ResultController#result')->name('showstudentresult');
Route::resource('results','ResultController');
chooseclassbatchresult.blade.php
#extends('layout')
#section('content')
<h1>Please Choose the Class and Respected Batch Of Student For Result</h1>
</br>
</br>
<form action="{{route('showstudentresult')}}" method="post">
#csrf
<p>
<label>Class Name</label>
<select name='class'>
#foreach($classes as $class)
<option value="{{$class->id}}">{{$class->name}}</option>
#endforeach
</select>
</br>
</p>
<p>
<label>Batch</label>
<select name='batch'>
#foreach($batches as $batch)
<option value="{{$batch->id}}">{{$batch->batch}}</option>
#endforeach
</select>
</p>
</br>
<input type="submit" value="View">
</form>
</br>
</br>
</br>
<h1>OR</h1>
<h3>
<button><a href={{route('students.create')}}>Add New Student</a></button>
</h3>
#endsection
Showstudentresult.blade.php
#extends('layout')
#section('content')
<table border=1>
<thead>
<tr>
<th>S.N</th>
<th>Name</th>
<th>Roll NO</th>
#foreach($class->subjects as $subject)
<th>{{$subject->name}}</th>
#endforeach
<th>Total Mark</th>
<th>Percentage</th>
<th>Division</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $id = 1; ?>
#foreach($students as $student)
<tr>
<td><?php echo $id;?></td>
<td>{{$student->name}}</td>
<td>{{$student->roll}}</td>
#foreach($student->subjects as $subject)
<th>{{$subject->pivot->mark}}</th>
#endforeach
<td>{{$student->result->total_mark}}</td>
<td>{{$student->result->percentage}}</td>
<td>{{$student->result->division}}</td>
<td>
<table>
<tr>
<td>
<button>Edit</button>
</td>
<td>
<form action="{{route('students.destroy',$student->id)}}" method="post">
#csrf
#method('DELETE')
<input type="submit" value="Delete"
onclick="return confirm('Are you sure you want to delete the student?')">
</form>
</td>
</tr>
</table>
</td>
</tr>
<?php $id++ ?>
#endforeach
</tbody>
</table>
</br>
</br>
<button><a href={{results.create}}>Create New</a></button>
#endsection
As Ross Wilson suggested in comment
I would suggest creating a separate page similar to
chooseclassbatchresult with a form that submits the data to create
Add a route in your routes files like:
Route::post('/createPage', 'ResultController#createPage')->name('createPage');
In ResultController add the following function:
public function createPage(Request $request)
{
// Get your required ids here
$classid = $request->class;
$batchid = $request->batch;
//dd($classid);
//dd($batchid );
}
In your chooseclassbatchresult view add another form like below
<form action="{{ route('createPage') }}" method="post">
#csrf
<p>
<label>Class Name</label>
<select name='class'>
#foreach($classes as $class)
<option value="{{$class->id}}">{{$class->name}}</option>
#endforeach
</select>
</br>
</p>
<p>
<label>Batch</label>
<select name='batch'>
#foreach($batches as $batch)
<option value="{{$batch->id}}">{{$batch->batch}}</option>
#endforeach
</select>
</p>
</br>
<input type="submit" value="View">
</form>
Thank you for your response. I got a way around my question. I learned that you can use input type="hidden" to carry those values back to the controller.
Create a route:
Route::post('/create_res', 'ResultController#create_res')->name('results.create_res');
In the View, chooseclassbatchresult.blade.php
<form action="{{route('results.create_res')}}" method="POST">
#csrf
<input type="hidden" name="classid" value="{{$classid}}">
<input type="hidden" name="batchid" value="{{$batchid}}">
<input type="submit" value="Create Mark Sheet">
</form>
In the Result Controller;
public function create_res(Request $request){
$classid = $request->classid;
$batchid = $request->batchid;
$students = Student::where('sclass_id',$classid)
->where('batch_id',$batchid)
->whereDoesntHave('subject')
->get();
$classes= Sclass::findOrFail($classid);
//dd($students);
return view('addmarksheet',compact('students','classes'));
}

Checkbox Array Validation Codeigniter

On my form each row has it's on submit button and you need to check the check box before you can delete it else it should through error.
Question: My checkbox is in_array but if I do not check the box and then press submit it does not through the codeigniter form_validation error. I have used $this->form_validation->set_rules('selected[]', 'Selected', 'required'); But error not showing up.
What is the best solution in making it getting form_validation error to work?
View
<?php echo form_open('admin/design/layout/delete'); ?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td style="width: 1px;" class="text-center">
<input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" />
</td>
<td>Layout Name</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($layouts) { ?>
<?php foreach ($layouts as $layout) { ?>
<tr>
<td class="text-center"><?php if (in_array($layout['layout_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" />
<?php } ?>
</td>
<td><?php echo $layout['name']; ?></td>
<td class="text-right">
<button type="submit" class="btn btn-danger">Delete</button>
Edit
</td>
</tr>
<?php } ?>
<?php } else { ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php echo form_close(); ?>
Controller Function
public function delete() {
$this->load->library('form_validation');
$this->form_validation->set_rules('selected[]', 'Selected', 'required');
if ($this->form_validation->run() == FALSE) {
echo "Error";
$this->get_list();
} else {
$selected_post = $this->input->post('selected');
if (isset($selected_post)) {
foreach ($selected_post as $layout_id) {
}
echo "Deleted $layout_id";
$this->get_list();
}
}
}
It won't validate per field. selected[] selector in rules means, when you submit your form, it should be at least one selected item. Now you have submit buttons, which are independently submit the form, no matter where are they in the dom, and which checkboxes are selected.
Currently it the same, as you would have one submit button at the end.
I would add some javascript, and set if the checkbox is not selected, you can disable that field:
<script>
$(function() {
$('input:checkbox').on('change', function() {
if($(this).is(':checked')) {
$(this).closest('tr').find('button:submit').prop('disabled', false);
}
else {
$(this).closest('tr').find('button:submit').prop('disabled', true);
}
})
})
</script>
And add disabled to your submit buttons:
<button type="submit" class="btn btn-danger" disabled>Delete</button>
It's not a server side validation, but you can achieve to prevent push the button next unchecked checkboxes.

Codeigniter File Doesn't get Uploaded

I have the following form and controller where it has a image upload, but everything goes smooth except the file not being uploaded to the particular folder.
View
<?php
$this->load->helper('url');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>diluks eCommerce cms - home page</title>
<link href="<?php
echo base_url();
?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="<?php echo base_url();?>index.php/addproduct_controller" method="post">
<?php
include 'header-adminpanel.php';
?>
<div class="container">
<div class="body-content">
<div class="side-left"><?php
include 'adminproduct_sidebar.php';
?></div>
<div class="side-right">
<br />
<table>
<tr>
<td class="captions">Product Code</td>
<td><input name="txt_pcode" type="text"/></td>
</tr>
<tr>
<td class="captions">Product Name</td>
<td><input name="txt_pname" type="text" size="40" /></td>
</tr>
<tr>
<td class="captions">Product Price</td>
<td><input name="txt_pprice" type="text" /></td>
</tr>
<tr>
<td class="captions">Product Description</td>
<td><textarea name="txt_pdesc" style="width:300px;height:100px;"></textarea></td>
</tr>
<tr>
<td class="captions">Product Image</td>
<td><input type="file" name="userfile" size="20" /></td>
</tr>
<tr>
<td class="captions">Product Options</td>
<td><input name="txt_poptions" size="40" type="text" /><a class="hint"> (Separate by a "," comma)</a></td>
</tr>
<tr><td><input name="btn_add" class="button" type="submit" value="Add" /></td></tr>
</table>
<br />
</div>
</div>
</div>
<div style="clear:both"></div>
<?php
include 'footer.php';
?>
</form>
</body>
</html>
Controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Addproduct_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
if (isset($_POST["btn_logout"])) {
$this->session->sess_destroy();
$this->load->view('welcome_view');
} else if (isset($_POST["btn_home"])) {
$this->load->view('welcome_view');
} else if (isset($_POST["btn_account"])) {
} else if (isset($_POST["btn_add"])) {
$prod_img = 'no image';
$config['upload_path'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
// $error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
//return 'error';
} else {
global $prod_img;
$data = array(
'upload_data' => $this->upload->data()
);
$prod_img = $data->file_name;
// $this->load->view('upload_success', $data);
}
$prod_name = $_POST["txt_pname"];
$prod_code = $_POST["txt_pcode"];
$prod_price = $_POST["txt_pprice"];
$prod_desc = $_POST["txt_pdesc"];
$prod_options = $_POST["txt_poptions"];
$this->load->model('product_model');
$addproduct_result = $this->product_model->addProduct($prod_code, $prod_name, $prod_price, $prod_desc, $prod_img);
if ($addproduct_result == true) {
echo "Added Successfully";
} else {
echo "Failed";
}
}
}
}
Then I tried by adding following instead of normal tags.
<?php
$this->load->helper('form');
?>
<?php
echo form_open_multipart(base_url().'index.php/addproduct_controller');
?>
where it gaves me an error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/addproduct_controller.php
Line Number: 53
Please help me with this or show me where I have done the mistake.
enctype attribute missing in your from tag.
Add enctype="multipart/form-data" in your form tag
OR
In CI, Use form_open_multipart function to generate form tag
As per discussion in comment, update your code as below.
$data = array(
'upload_data' => $this->upload->data()
);
$prod_img = $data["upload_data"]->file_name;
You have missed out to include form attribute to upload the file
Add enctype="multipart/form-data" in your form tag
You have make the html form but not added file upload tag in your form creation.
add: enctype="multipart/form-data" in your form tag.
<form action="<?php echo base_url();?>index.php/addproduct_controller" method="post" enctype="multipart/form-data" >

JToolbar frontend submit value on click

I'm building a joomla component and I can't find a solution to the following. In my front end I'm using the joomlas build in class JToolbar to handle events on click like edit, delete so one.
<form action="<?php echo JRoute::_('index.php');?>" method="post"
name="termForm" id="adminForm">
<table class="stripeMe">
<tbody>
<thead>
<tr>
<th>Begriff</th>
<th>Definition</th>
<?php if ($user->authorize('com_glossary', 'edit', 'glossary', 'all')): ?><th>Published</th> <?php endif; ?>
</tr>
</thead>
<?php foreach($this->items as $i => $item): ?>
<tr>
<td>
<span class="title"><?php echo $item->tterm; ?></span>
<?php if ($user->authorize('com_glossary', 'edit', 'bearbeiten', 'all')):?>
<?php echo $this->getEdit(); ?><?php endif; ?>
</td>
<td><?php echo $item->tdefinition; ?></td>
<?php if ($user->authorize('com_glossary', 'edit', 'bearbeiten', 'all')): ?>
<td><?php echo $this->getPublished(); ?></td> <?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div>
<input type="hidden" name="task" value="" /> <input type="hidden"
name="id" value="" onclick="submitbutton(<?php echo count( $item->id ); ?>);" /> <input type="hidden"
name="option" value="com_glossary" /> <input type="hidden"
name="controller" value="bearbeiten" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
I would like to pass to id of the selected row to the sub-controller on button event and I don't really know how to do it
Here you have some useful tips about using JToolbar on the frontend http://docs.joomla.org/How_to_use_the_JToolBar_class_in_the_frontend
I have done it once in the past, and from what I remember I did some tricks in order to make it work.
1.) Firstly remove the "id" input and add the following one at the end of your form:
<input type="hidden" name="boxchecked" value="0" />
2.) Secondly make sure Mootools is attached to the source
3.) Finally: There, where you started your foreach loop, after "tr" tag add another table column:
<td><?php echo JHTML::_('grid.id', $i, $item->id ); ?></td>
Don't forget to create a column heading in thead for this column.
These steps will create a checkbox in the first cell of every row and make the form able to send selected field's id with request.
edit:
tbody tag is in wrong place, it's supposed to be after thead tag. Also there is no use of attaching events to hidden input as they won't be triggered
Cheers
Peter

Resources