Cart block not updating codeigniter - javascript

I have this controller:
<?php
class Cart extends CI_Controller{
public $paypal_data = '';
public $tax;
public $shipping;
public $total = 0;
public $grand_total;
/*
*Cart Index
*/
public function index(){
//Load view
$data['main_content'] = 'cart';
$this->load->view('layouts/main', $data);
}
/*
*Add To Cart
*/
public function add(){
//Item data
$data = array(
'id'=> $this->input->post('item_number'),
'qty'=> $this->input->post('qty'),
'price'=> $this->input->post('price'),
'name'=> $this->input->post('title'),
);
//print_r($data);die();
//Insert Into cart-block
$this->cart->insert($data);
redirect('products');
}
}
And I have the view file which uses this controller:
<div class="cart-block">
<form action="cart/update" method="post">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i. '[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" name="<?php echo $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size= "5"</td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go To Cart</a></p>
</form>
</div>
<div class="panel panel-default panel-list">
<div class="panel-heading panel-jeading-dark">
<h3 class="panel-title">
Categories
</h3>
</div>
<!--List group -->
<ul class="list-group">
<?php foreach(get_categories_h() as $category) : ?>
<li class="list-group-item"><?php echo $category->name; ?></li>
<?php endforeach; ?>
</ul>
I apologize that I can't show you the actual website because it's not online but based on the above snippets can any point out an error in the view file or controller that causes the shopping cart not to update?
I'm following a video tutorial but it seems I'm doing something wrong. Here is the video: https://youtu.be/ajt_DJMS5FM?t=13m45s

Your form action is cart/update your function in the controller is add?
Change the form action to cart/add. Or write an cart/update function.

Firstly change the Form Action action="cart/update" to
action="<?php echo base_url();?>cart/add" and apply die(); and test it

load the cart library in controller
$this->load->library('cart');

Related

Get the unique total scores of students

I have a form on my school web application where teachers are able to add scores for students and at the end of each row, the total, which is the sum of input values is automatically calculated per student.
The problem is that it output the total of all the students instead of the total of individual students on all the rows even ones with no scores added.
How do I get the unique total value for each row?
Below is my code
<?php if($class_id >= 1 && $class_id <= 4 && $student['is_secmid'] == 1){?>
<form method="post" action="<?php echo site_url('admin/examgroup/entrymarks') ?>" id="assign_form1111">
<input type="hidden" id="max_mark" value="<?php echo $subject_detail->max_marks; ?>">
<?php
if (isset($resultlist) && !empty($resultlist)) {
?>
<div class="row">
<div class="col-md-12">
<input type="hidden" name="exam_group_class_batch_exam_subject_id" value="<?php echo $exam_group_class_batch_exam_subject_id; ?>">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>C.A 1 (10.0)</th>
<th>C.A 2 (10.0)</th>
</tr>
</thead>
<tbody>
<?php if (empty($resultlist)) {
?>
<tr>
<td colspan="7" class="text-danger text-center"><?php echo $this->lang->line('no_record_found'); ?></td>
</tr>
<?php
} else {
foreach ($resultlist as $student) {
?>
<tr class="std_adm_<?php echo $student['admission_no']; ?>">
<input type="hidden" name="prev_id[<?php echo $student['exam_group_class_batch_exam_students_id'] ?>]" value="<?php echo $student['exam_group_exam_result_id'] ?>">
<input type="hidden" name="exam_group_student_id[]" value="<?php echo $student['exam_group_class_batch_exam_students_id'] ?>">
<td><?php echo $student['admission_no']; ?></td>
<td style="white-space: nowrap;"><?php echo $student['lastname'] . " " . $student['firstname']; ?></td>
<td> <input type="number" class="marksssss2 form-control" min="0" max="10" name="exam_group_student_ca1_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_get_ca1']; ?>" step="any"></td>
<td> <input type="number" class="marksssss3 form-control" min="0" max="10" name="exam_group_student_ca2_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_get_ca2']; ?>" step="any"></td>
<td> <output class="result"></output></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
<?php if ($this->rbac->hasPrivilege('exam_marks', 'can_edit')) { ?>
<button type="submit" class="allot-fees btn btn-primary btn-sm pull-right" id="load" data-loading-text="<i class='fa fa-spinner fa-spin '></i> Please Wait.."><?php echo $this->lang->line('save'); ?>
</button>
<?php } ?>
</div>
</div>
<?php } ?>
<script>
const $inputs = $('input[type="number"]')
$inputs.change(function() {
var total = 0;
var parent = $(this).closest('.row');
parent.find('input[type="number"]').each(function() {
if ($(this).val() != '') {
total += parseInt($(this).val());
}
});
parent.find('.result').html(total);
});
</script>
you missed class row on <tr> and you are trying to find it on closest div from javascript, and you are getting this (<div class="row">), it have all the (<input type="number").
you forget to add class to TR. Add class row on <tr> so you can get all (<input type="number") inside the tr.
because of not added class on TR you are facing issue that all total have same numbers.

Clicking on any radio button in my created list results in only the first <select> field being activated

Here is the video in specific from what i mean. It is hosted on my personal drive
video
Here is my code
<?php
$pagetitle="Search Report";
include "includes/header.php";
include("config.php");
include("connection.php");
error_reporting(E_ALL ^ E_DEPRECATED);
$name=$_GET['filo'];
$ksm=$_GET['kisim'];
$drs=$_GET['hders'];
$ndrs=$_GET['dname'];
$tch=$_GET['tname'];
?>
<script>
//Here Is javascript code
function enable() {
document.getElementById('option').disabled=false;
}
</script>
<style type="text/css">
input[type="radio"]{
margin: 0px 4px 0px;
}
</style>
<div class="container">
<div class="row">
<div class="templatemo-line-header" style="margin-top: 0px;" >
<div class="text-center">
<hr class="team_hr team_hr_left hr_gray"/><span class="span_blog txt_darkgrey txt_orange"> </span>
<hr class="team_hr team_hr_right hr_gray" />
</div>
</div>
</div>
<div class="table-responsive">
<table class="ui celled table table table-hover">
<form action="check.php" method="get">
<thead>
<tr>
<th>Adı ve Soyadı</th>
<th>Dersin Adı</th>
<th>Dersin Saatı</th>
<th>Öğretim Elemanı</th>
<th>Durum</th>
<th>Sebepi</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<?php
$query3=mysqli_query($connect,"select * from student where filo='$name' and kisim='$ksm' ORDER BY student_name");
$arra=-1;
while($row=mysqli_fetch_row($query3))
{
$arra++;
?>
<tr>
<td><?php echo $row[1] ?></td>
<input type="hidden" value="<?php echo $row[1] ?>" name="std_name[]">
<td><?php echo $ndrs ?></td>
<input type="hidden" value="<?php echo $ndrs ?>" name="ndrs[]">
<td><?php echo $drs ?></td>
<input type="hidden" value="<?php echo $drs?>" name="drs[]">
<td><?php echo $tch ?></td>
<input type="hidden" value="<?php echo $tch ?>" name="tch[]">
<td>
///////HERE IS ONE PART OF THE CODE
<input type="radio" name="attendance[<?php echo "$arra" ?>]" value="Mevcut" checked="checked" >Mevcut
<input type="radio" name="attendance[<?php echo "$arra" ?>]" onclick="enable()" value="Gayri">Gayrı
</td>
<td>
<select class="form-control" id="option" name="option[<?php echo "$arra" ?>]" disabled >
<option selected value="Mevcut">Mevcut</option>
<option value="Vizite">Vizite</option>
<option value="Hst Muayene">Hst Muayene</option>
<option value="Izinli">Izinli</option>
</select>
</td>
<td><?php echo $row[0] ?>
<input type="hidden" value="<?php echo $row[0] ?>" name="id[]">
</td>
</tr>
<?php } ?>
</tbody>
</table>
<button type="submit" class="btn btn-success btn-lg btn-block" name="submit2">Gönder</button>
</form>
</div><!--table-responsive-->
</div><!--row-->
</div><!--container-->
<?php include "includes/footer.php"; ?>
I try to explain my problem.
Scenario is like that. Some infos are taken from a submit form and with this infos i create a list then created list has 2 radio buttons and by default a disabled select area.
The problem is here. When i click on "Gayri" button field must be activated for every person in that list instead of that just the first field is activated.
Can anyone help me? I want for every possible person in the list when i click on "gayri" radio button the field to be activated.

Can I put <form> in table as a modal

I have a question about form in table if I put the form inside the table as a modal there is a red bar and dont equal to the end tag form, I would like to know if im following rules or bad coding in html?
<table>
<thead>
<tr>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<?php
$conn = mysqli_connect($servername, $username, $password, $database);
$id=$_GET["id"];
$sqls = "SELECT resident_id, complaints_id, nature_of_complaints, status
FROM table_complaints WHERE resident_id=?";
mysqli_stmt_store_result($stmt );
if (mysqli_stmt_num_rows($stmt ) > 0) {
while(mysqli_stmt_fetch($stmt )) { ?>
<tr>
<td style="text-transform: capitalize"><?php echo
substr($nature_of_complaints,0,8);?>..</td>
<td style="text-transform: capitalize"><?php
echo $status?></td>
<td><a href="#" onclick="document.getElementById('id016<?php echo
$complaints_id?>').style.display='block'" >Add</a></td>
</tr>
<div id="id016<?php echo $complaints_id?>" class="modal">
<div class="modal-content">
<header>
<p>Involvement</p>
</header>
<form action="involvement.php" method="POST">
<input name="resid" value="<?php echo $resident_id?>" type="hidden">
<input name="compid" value="<?php echo $complaints_id?>" type="hidden">
<input type="submit" name="submit" value="Add">
</form>
</div>
<footer>
<button type="button" onclick="document.getElementById('id016<?php echo
$complaints_id?>').style.display='none'">Close</button>
</footer>
<?php
}
} else { ?>
<tbody><tr><td><?php echo "No Complaints";?></td></tr></tbody>
<?php
}
</table>

Unable to use Jquery.load() feature properly

I am trying to filter results that will be displayed to user based on selection, but, its not displaying, properly, I do not know what I am doing wrong. I process using GET on the same page (users.php?rid=). This is how it displays when I select a value. Seems it loads the page twice into the div
The Select Button
<select name="role_id" id="role_id" style="width: 150px;"
onchange="">
<option value="">Select User</option>
<?php
$rid = 0;
if (isset($_GET['rid'])) {
$rid = clean($_GET['rid']);
}
$roles = get_user_roles();
foreach ($roles as $row):
?>
<option
value="<?php echo $row['role_id']; ?>"<?php if (isset($rid) && $rid == $row['role_id']) echo 'selected'; ?>>
<?php echo $row['name']; ?>
</option>
<?php endforeach; ?>
</select>
RESULT DIV
<div id="filter_result">
<table class="table table-striped" >
<thead>
<tr>
<th><div>#</div></th>
<th><div>Name</div></th>
<th><div>Email</div></th>
<th><div>User Type</div></th>
<th><div>Options</div></th>
</tr>
</thead>
<tbody>
<?php
if ($rid < 1) {
$users = get_users();
} else {
$users = get_role_users($rid);
}
if ($users->num_rows < 1) {
echo '<div class = "alert alert-warning text-center">There are no Students for this Class.... <a class="btn btn-primary btn-sm" href="add_user.php">Add User</a></div>';
}
$count = 1;
foreach ($users as $row):?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo get_role_name($row['role_id']); ?></td>
<!--Options-->
<td>
<!--Change Role-->
<div class="btn-group">
<button type="button" class="btn btn-primary btn-sm dropdown-toggle"
data-toggle="dropdown">
Change Role <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-default pull-right" role="menu">
<?php $roles = get_user_roles();
foreach ($roles as $row2):
?>
<li>
<a href="change_roles.php?rid=<?php echo $row2['role_id'].'&uid='.$row['user_id'];?>">
<?php echo $row2['name']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!--Delete Student-->
<a href="delete_user.php?id=<?php echo $row['user_id']; ?>"
onclick="confirm('Are You sure?')" class="btn btn-danger"> Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div
JAVASCRIPT
<script>
$(document).on('change', 'select#role_id', function () {
var role_id = $(this).val();
$('#filter_result').load('users.php?rid='+role_id, '#filter_result');
});
</script>
It should display like this
Since you are Checking selection on the same page, you can assign id to your body tag, for example
<body id="page_body">
then use #page_body.load(), it will work
$('#page_body').load('users.php?rid='+role_id, '#filter_result');

PHP Looping Javascript Just Show The First Record

I have a view code:
<?php
$i = 1;
//var_dump($this->productList);
foreach ($this->productList as $data) {
$desc=explode(",",$data['descriptions']);
?>
<tr>
<th colspan="3">
<input type="hidden" id="id_pack" name="id_pack" value="<?php echo $data['package_id']; ?>">
<input type="hidden" id="nama_pack" name="nama_pack" value="<?php echo $data['package_name']; ?>">
<h4 align="center" class="title-pack"><?php echo $data['package_name']; ?></h4>
</th>
</tr>
<tr id="dashe">
<td>
<ul class="myul">
<?php foreach($desc as $descriptions) { ?>
<li class="myli"> <?php echo $descriptions; ?></li>
<?php } ?>
</ul>
</td>
<td>
<h4 class="prize">
Rp. <?php echo number_format($data['price'],2);?>
/ month
</h4>
</td>
<td>
<p id="order" name="order" class="mybutton" data-toggle="modal" data-target=".mymodal">Order</p>
</td>
</tr>
<?php
$i++;
}
?>
And this the javascript:
document.getElementById("order").addEventListener("click", tampilkanHrm);
function tampilkanHrm() {
var pilihan=$("#nama_pack").val();
document.getElementById("choice").innerHTML = pilihan;
document.getElementById("pack").value = pilihan+" Package";
}
the problem is, in the order button it always show the same record. Just show the first data, even i clicked other order button. Hope somebody help.
You can't assign same id for multiple elements.
So, have different ids.
<?php
$i = 1;
//var_dump($this->productList);
foreach ($this->productList as $data) {
$desc=explode(",",$data['descriptions']);
?>
<tr>
<th colspan="3">
<input type="hidden" id="id_pack<?php echo $i ?>" name="id_pack" value="<?php echo $data['package_id']; ?>">
<input type="hidden" id="nama_pack<?php echo $i ?>" name="nama_pack" value="<?php echo $data['package_name']; ?>">
<h4 align="center" class="title-pack"><?php echo $data['package_name']; ?></h4>
</th>
</tr>
Similarly create buttons with different ids.
<td>
<p id="<?php echo $i ?>" name="order" class="mybutton" data-toggle="modal" data-target=".mymodal">Order</p>
</td>
Then change your javascript to jquery. Hope, you might also be using jquery [ understood from your code ]
$(".mybutton").on("click", function() {
var identifier = $(this).attr("id");
var pilihan=$("#nama_pack"+identifier).val();
$("#choice").html(pilihan);
});
Change your code like this and check.

Categories