Wordpress button in wo-xml feed administration not working - javascript

Problem is in administration console of wordpress in plugin 'woocommerce xml feeds'. There is button to add parameter for category. This button do nothing when I click it.
I tried to contact the author of plugin, he only told me to check debug console, but there is nothing. He also told me that function of this button depends on jQuery.
This is the button element:
<span class="btn btn-danger btn-sm add-param" data-par="80">Přidat parametr</span>
The whole PHP file for context:
<?php
/**
*
* #package woo_xml_feeds
* #author Vladislav Musilek
* #license GPL-2.0+
* #link http://musilda.cz
* #copyright 2014 Vladislav Musilek
*
* Version 1.0.0
*
*/
if(isset($_POST['update'])){
//Save Heureka.sk Shipping values
$this->save_heureka_delivery($name = 'woo_heureka_delivery');
//Save Heureka.cz CPC value
$this->save_single_option('heureka-cpc','heureka-cpc');
/**
* Save Heureka category
*/
$heureka_assing_categories = get_option('woo_heureka_assing_categories');
if(empty($heureka_assing_categories)){ $heureka_assing_categories = array(); }
$heureka_excluded_categories = get_option( 'woo_heureka_excluded_categories');
if(empty($heureka_excluded_categories)){ $heureka_excluded_categories = array(); }
$heureka_categories_cpc = get_option( 'woo_heureka_categories_cpc');
if(empty($heureka_categories_cpc)){ $heureka_categories_cpc = array(); }
foreach($_POST['termid'] as $key => $item){
if(!empty($_POST['heurekaid'])){
if(!empty($_POST['heurekaid'][$key])){
$heureka_assing_categories[$_POST['termid'][$key]] = $_POST['heurekaid'][$key];
}else{
if(!empty($heureka_assing_categories[$_POST['termid'][$key]])){
unset($heureka_assing_categories[$_POST['termid'][$key]]);
}
}
}
if(!empty($_POST['excluded'][$key])){
$heureka_excluded_categories[$_POST['termid'][$key]] = $_POST['termid'][$key];
}else{
if(!empty($heureka_excluded_categories[$_POST['termid'][$key]])){
unset($heureka_excluded_categories[$_POST['termid'][$key]]);
}
}
if(!empty($_POST['termvar'][$key])){
$heureka_categories_cpc[$_POST['termid'][$key]] = $_POST['category_cpc'][$key];
}else{
if(!empty($heureka_categories_cpc[$_POST['termid'][$key]])){
unset($heureka_categories_cpc[$_POST['termid'][$key]]);
}
}
}
update_option( 'woo_heureka_assing_categories', $heureka_assing_categories );
update_option( 'woo_heureka_excluded_categories', $heureka_excluded_categories );
update_option( 'woo_heureka_categories_cpc', $heureka_categories_cpc );
//Parametry pro kategorie
$cat_params = get_option( 'woo_heureka_cat_params');
if(empty($cat_params)){ $cat_params = array(); }
foreach($_POST['termvar'] as $key => $item){
$cat_params[$item]['term_id'] = $item;
$cat_params[$item]['parametry'] = array();
if(!empty($_POST['nazev_parametru_'.$item])){
foreach($_POST['nazev_parametru_'.$item] as $lit => $var){
$cat_params[$item]['parametry'][$lit]['nazev_parametru'] = $_POST['nazev_parametru_'.$item][$lit];
$cat_params[$item]['parametry'][$lit]['hodnota_parametru'] = $_POST['hodnota_parametru_'.$item][$lit];
}
}
}
update_option( 'woo_heureka_cat_params', $cat_params );
if(isset($_GET['catoffset'])){
wp_redirect(admin_url().'admin.php?page=heureka-cz&catoffset='.$_GET['catoffset']);
}else{
wp_redirect(admin_url().'admin.php?page=heureka-cz');
}
}
$cat_params = get_option( 'woo_heureka_cat_params');
//Get heureka cz categories
$heureka_categories = get_option( 'woo_heureka_categories');
$heureka_assing_categories = get_option( 'woo_heureka_assing_categories');
$heureka_excluded_categories = get_option( 'woo_heureka_excluded_categories');
$heureka_categories_cpc = get_option( 'woo_heureka_categories_cpc');
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC'));
$cat_list = custom_taxonomy_walker('product_cat');
$use_select2 = get_option( 'woo_xml_feed_use_select2' );
/**
* Cat pagging
*
*/
$limit = 50;
if(isset($_GET['catoffset'])){
$catstart = (($_GET['catoffset'] * $limit) - $limit)+1;
$catend = $_GET['catoffset'] * $limit;
}else{
$catstart = 1;
$catend = $limit;
}
$i = 1;
$ii = 1;
?>
<div class="wrap">
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<form method="post" action="">
<?php
//Include Heureka CPC table
include('modules/heureka_cz_cpc.php');
//Include Heureka Shipping table
include('modules/heureka_cz_shipping.php');
//Display category setting paggination
echo tax_pagination($cat_list, 50);
?>
<!-- Heureka kategorie -->
<div class="t-col-12">
<div class="toret-box box-info">
<div class="box-header">
<h3 class="box-title"><?php _e('Přiřazení kategorií','woo-xml-feeds'); ?></h3>
</div>
<div class="box-body">
<p><?php _e('Zde můžete přiřadit všechny kategorie ve vašem obchodu, k jednotlivým kategoriím Heuréky. Pokud máte v eshopu větší množství kategorií, buďte prosím trpěliví, načtení může chvíli trvat. Po rozkliknutí výběru Heuréka kategorie, můžete do řádku zapsat počáteční písmena hledané kategorie a použít našeptávač.','woo-xml-feeds'); ?></p>
<p><?php _e('Pokud nechcete zobrazovat zboží z určité kategorie v XML feedu, zaškrtněte "Vyloučit kategorii".','woo-xml-feeds'); ?></p>
<table class="table-bordered">
<tr>
<th><?php _e('Vyloučit kategorii', 'woo-xml-feeds'); ?></th>
<th><?php _e('Kategorie obchodu', 'woo-xml-feeds'); ?></th>
<th><?php _e('Kategorie na Heuréce', 'woo-xml-feeds'); ?></th>
<th><?php _e('CPC kategorie', 'woo-xml-feeds'); ?></th>
</tr>
<?php
$catTerms = explode(',',$cat_list);
$aa = 0;
foreach($catTerms as $c_item) :
if(!empty($c_item) && $i >= $catstart){
if($i > $catend){ break; }
?>
<tr>
<td class="td_center"><input class="icheck_red" type="checkbox" name="excluded[<?php echo $aa; ?>]" <?php if(!empty($heureka_excluded_categories[$c_item])){ echo 'checked="checked"'; } ?> value="<?php echo $c_item; ?>" ></td>
<td>
<?php
$aa++;
$catTerm = get_term_by( 'id', $c_item, 'product_cat' );
if(!empty($catTerm->parent)){
$p_name = get_term_by( 'id', $catTerm->parent, 'product_cat' );
echo $p_name->name.' >> ';
}
?> <?php echo $catTerm->name; ?>
<input type="hidden" name="termid[]" value="<?php echo $catTerm->term_id; ?>" />
</td>
<td>
<?php
if(!empty($use_select2) && $use_select2 == 'no'){}else{
?>
<script>
jQuery(document).ready(function() { jQuery("#heureka<?php echo $i; ?>").select2(); });
</script>
<?php } ?>
<style>#s2id_heureka<?php echo $i; ?>{min-width:800px;}</style>
<select name="heurekaid[]" id="heureka<?php echo $i; ?>">
<option value="default"></option>
<?php
foreach($heureka_categories as $key => $item){
if(!empty($item['category_fullname'])){
?>
<option <?php if(!empty($heureka_assing_categories[$catTerm->term_id]) && $heureka_assing_categories[$catTerm->term_id]==$key){ echo 'selected="selected"'; }; ?>value="<?php echo $key; ?>"><?php echo $item['category_fullname']; ?></option>
<?php }
}
?>
</select>
</td>
<td><input type="text" name="category_cpc[]" value="<?php if(!empty($heureka_categories_cpc[$c_item])){ echo $heureka_categories_cpc[$c_item]; } ?>" style="width:40px;" /></td>
</tr>
<?php
}
$i++;
endforeach;
?>
</table>
</div>
</div>
</div>
<!-- Heureka kategorie parametry -->
<div class="t-col-12">
<div class="toret-box box-info">
<div class="box-header">
<h3 class="box-title"><?php _e('Vlastní parametry pro kategorie','woo-xml-feeds'); ?></h3>
</div>
<div class="box-body">
<table class="table-bordered">
<tr>
<th><?php _e('Kategorie eshop', 'woo-xml-feeds'); ?></th>
<th style="width:80%;"><?php _e('Vlastní parametry', 'woo-xml-feeds'); ?></th>
</tr>
<?php
$catTerms = explode(',',$cat_list);
foreach($catTerms as $c_item) :
if(!empty($c_item) && $ii >= $catstart){
if($ii > $catend){ break; }
?>
<tr>
<td>
<?php
$catTerm = get_term_by( 'id', $c_item, 'product_cat' );
if(!empty($catTerm->parent)){
$p_name = get_term_by( 'id', $catTerm->parent, 'product_cat' );
echo $p_name->name.' >> ';
}
?> <?php echo $catTerm->name; ?>
<input type="hidden" name="termvar[<?php echo $catTerm->term_id; ?>]" value="<?php echo $catTerm->term_id; ?>" />
</td>
<td class="category_params">
<?php
if(!empty($cat_params[$catTerm->term_id]['parametry'])){
foreach($cat_params[$catTerm->term_id]['parametry'] as $lit => $var){ ?>
<fieldset>
<input type="text" name="nazev_parametru_<?php echo $catTerm->term_id; ?>[]" placeholder="Název parametru" value="<?php echo $var['nazev_parametru']; ?>" />
<input type="text" name="hodnota_parametru_<?php echo $catTerm->term_id; ?>[]" placeholder="Hodnota parametru" value="<?php echo $var['hodnota_parametru']; ?>"/>
<span class="btn btn-danger btn-sm remove-param"><i class="fa fa-times"></i></span>
</fieldset>
<?php }
}
?>
<div class="clear"></div>
<span class="btn btn-danger btn-sm add-param"data-par="<?php echo $catTerm->term_id; ?>">Přidat parametr</span>
</td>
</tr>
<?php
}
$ii++;
endforeach;
?>
</table>
<input type="hidden" name="update" value="ok" />
<input type="submit" class="btn btn-lg btn-warning" value="<?php _e('Uložit nastavení','woo-xml-feeds'); ?>" />
</div>
</div>
</div>
</form>
</div>
I really tried to google it I found similar problems with some simple advice to switch to default theme or to put define('CONCATENATE_SCRIPTS', false); to wp-config.php. No luck with any of these.

Related

Sweetalert won't show but it will update

I want to show a sweetalert after clicking the set button but it won't function. This is my index page and the set button can function but it won't show the sweet alert. what might be the problem and what should I do?
index.php
<form method='post' action='updataStatus.php'>
<button type='submit' name='but_update' class="inline-block float ml-2 mt-1 btn-group pull-right btn-danger btn-sm">SET</button><button type="submit" id="dataExport" name="dataExport" value="Export to excel" class="inline-block float ml-2 mt-1 btn-group pull-right btn-info btn-sm">Export</button>
<div class="table-responsive">
<br>
<tbody><table class="table table-hover table-bordered" id="sampleTable2">
<thead>
<tr>
<th><input type="checkbox" class="select-all checkbox" name="select-all" id="checkAll" /></th>
<th>Name</th>
<th>Scholarship Program</th>
<th>Course</th>
<th>Semester</th>
<th>Allowance</th>
</tr>
</thead>
<?php
require_once "connection.php";
$query = "SELECT * FROM allowance";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result) ){
$id = $row['id'];
$Name = $row['Name'];
$Scholarship = $row['Scholarship'];
$Course = $row['Course'];
$Semester = $row['Semester'];
$statusAllowance = $row['statusAllowance'];
?>
<tr>
<!-- Checkbox -->
<td><input type='checkbox' name='update[]' value='<?= $id ?>' ></td>
<td><p name="Name"><?php echo $row['Name']; ?></p></td>
<td><p name="Scholarship"><?php echo $row['Scholarship'] ?></p></td>
<td><p name="Course"><?php echo $row['Course'] ?></p></td>
<td><p name="Semester"><?php echo $row['Semester'] ?></p></td>
<td><p name='statusAllowance_<?= $id ?>'><?php echo $row['statusAllowance'] ?></td>
</tr>
<
?php
}
?>
</table>
</tbody>
<?php
if(isset($_SESSION['success']) && $_SESSION['success'] !='')
{
?>
<script type="text/javascript">
swal({
title: "<?php echo $_SESSION['success']; ?>",
icon: "<?php echo $_SESSION['status_code']; ?>",
button: "yissh",
});
</script>
<?php
unset($_SESSION['success']);
}
?>
This is my code on the edit part and this works, only the alert won't show up.
updataStatus.php
<?php
require_once "connection.php";
if(isset($_POST['but_update'])){
if(isset($_POST['update'])){
foreach($_POST['update'] as $id){
$statusAllowance = 'Received';
if($statusAllowance != '' ){
$updateUser = "UPDATE allowance SET statusAllowance='".$statusAllowance."' WHERE id=".$id;
$query_run = mysqli_query($conn,$updateUser);
if($query_run){
$_SESSION['success'] = "YOUR DATA UPDATED";
header('Location: tracking.php');
}else{
$_SESSION['success'] = "YOUR DATA IS NOT UPDATED";
header('Location: tracking.php');
}
}
}
}
}
?>
considering you have not implemented another function to call sweetalert; by default, it should be Swal.fire({}) not just swal({})
https://sweetalert2.github.io/

How to enable disable field with radio in looping

I have some problem in looping to make an action if radio with value = 1 then input field is enable.
here is my code, but imagine that the field and radio in a looping :
<form name="form" id="form">
<table>
<?php
$arr = array(0=>"Disable", 1=>"Enable");
for($i=0; $i<5; $i++):
?>
<tr>
<td><input type='text' name='input[]' id='input'></td>
<td>
<?php
foreach($arr as $key=>$val):
?>
<input type='radio' name='ans[<?php echo $i; ?>]' id='ans' value='<?php echo $key; ?>' onClick="d_e(this.value)"> <?php echo $val;
<?php endforeach; ?>
</td>
</tr>
<?php endfor; ?>
</table>
<script>
function d_e(id){
var i = document.form.input;
for(var c=0; c<i.length; c++){
if(id==1){
i[c].disabled = false;
}else{
i[c].disabled = true;
}
}
}
</script>

Updating quantity in cart table

I want that update button should be in that field which I choose.But it appears only in first row and is updating for first row quantity field.
<form action="cart.php?action=update" method="post">
<table>
<tr>
<th colspan="2">ITEM</th>
<th>QUANTITY</th>
<th>PRICE</th>
<th>SUBTOTAL</th>
<th>REMOVE</th>
</tr>
<?php
$query = "select * from cart where customer_id='$user' ";
$result = mysqli_query($con,$query);$b = 0;$c = 0;
while($row = mysqli_fetch_array($result))
{
$productid = $row['product_id'];
$query2 = "select * from product where product_id='$productid'";
$result2 = mysqli_query($con,$query2);
while($row2=mysqli_fetch_array($result2))
{
?>
<tr>
<td rowspan="3"><img src="upload/<?php echo $row2['pimage']; ?>" height="50px" width="50px"></td>
<td rowspan="3"><?php echo $row2['pname']; ?></td>
<td rowspan="3">
<input tpe="text" name="newqty" value="<?php echo $qty = $row['quantity']; ?>" onkeypress="showsubmit()">
<input style="visibility:hidden;width:80px;border-radius:10px;background-color:green;border:none;padding:5px;color:white;" type="submit" name="sub1" id="sub1" value="UPDATE">
<input type="hidden" name="hidcartid" value="<?php echo $row['cart_id'] ?>"/>
<input type="hidden" name="hidproductid" value="<?php echo $row['product_id']; ?>"/>
<script>
function showsubmit()
{
document.getElementById("sub1").style.visibility = "visible";
}
</script>
</td>
<td>Price:<?php echo $sp = $row2['psellingprice']; ?></td>
<?php
$total = $sp * $qty;
?>
<td rowspan="3">
<?php
echo $t = $total;
$b = $b + $t;
?></td>
<td rowspan="3">REMOVE</td>
</tr>
<?php
$action = ( array_key_exists( 'action', $_REQUEST) ? $_REQUEST['action'] : "" );
if($action =="delete")
{
deletecart($_REQUEST['cid']);
}
if($action=="update")
{
echo "update function called";
updatecart();
echo "update function executed";
}
?>
<tr>
<td>Selling Price:<?php echo $p = $row2['pprice']; ?></td>
</tr>
<tr>
<td>You Saved:
<?php
$d = $row2['pdiscount'];
$s = ($p*$d)/100;
echo $q = $s * $qty;
$c = $c + $q;
?> rs.</td>
</tr>
<?php
}
}
?>
</table>
and cart2.php
<?php
function deletecart($cartid)
{
include 'connection.php';
$sql1="delete from cart where cart_id=$cartid";
$executequery = mysqli_query($con,$sql1);
header('location:cart.php');
}
function updatecart()
{
include 'connection.php';
$cartId = $_POST['hidcartid'];
$productId = $_POST['hidproductid'];
$newqty = $_POST['newqty'];
echo("inside update function");
// update product quantity
$sql = "UPDATE cart
SET quantity = $newqty
WHERE cart_id = $cartId";
mysqli_query($con,$sql);
header('location:cart.php');
}?>
For row onw everything is fine.But for row2 update button appears in row1 and not updating at all.
<input tpe="text" name="newqty" value="<?php echo $qty = $row['quantity']; ?>" onkeypress="showsubmit(<?php echo $row['cart_id'] ?>)">
<input style="visibility:hidden;width:80px;border-radius:10px;background-color:green;border:none;padding:5px;color:white;"
type="submit"
name="sub_<?php echo $row['cart_id'] ?>"
id="sub_<?php echo $row['cart_id'] ?>"
value="UPDATE">
<script>
function showsubmit(id)
{
document.getElementById("sub_"+id).style.visibility ="visible";
}
</script>

Add to cart with ajax in codeigniter

I'm trying add to cart using CodeIgniter and it is working fine but, when I want to do the same through ajax its getting some problem. Can you please look at my codes and tell me where do I get some mistakes? I'm confused how to use ajax to call the add function of the controller . What should I add or do in the ajax code to make this function work?
<html>
<head>
<title>Codeigniter cart class</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Raleway:500,600,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<script type="text/javascript">
$(document).ready(function() {
$("#myform").submit(function(event) {
event.preventDefault();
var insert_data= $("#myform").serializeArray();
$.ajax({
url: "<?php echo base_url(); ?>" + "index.php/shopping/add",
type: "POST",
data: insert_data,
success: function(response)
{
if (response)
{
//window.location.replace("http://127.0.0.1/codeigniter_cart2/index.php/shopping");
window.location.href="http://127.0.0.1/codeigniter_cart2/index.php/shopping";
}
else{
alert('sorry');
}
}
});
});
});
</script>
</head>
<body>
<div id='content'>
<div class="row">
<div class="col-sm-5">
<h2 align="center">Items</h2>
<?php
?>
<table id="table" border="0" cellpadding="5px" cellspacing="1px">
<?php
foreach ($products as $product) {
$id = $product['serial'];
$name = $product['name'];
$price = $product['price'];
?>
<tr class="well">
<td style="padding-left:15px;"><?php echo $name; ?></td>
<td>
Rs. <?php echo $price; ?></td>
<?php
?>
<?php
echo form_open('',array('id' => 'myform'));
echo form_hidden('id', $id);
echo form_hidden('name', $name);
echo form_hidden('price', $price);
?> <!--</div>-->
<?php
$btn = array(
'class' => 'fg-button teal',
'value' => 'Add',
'name' => 'action',
'id' => 'add_button'
);
?>
<td>
<?php
// Submit Button.
echo form_submit($btn);
echo form_close();
?>
</td>
</tr>
<?php } ?>
</table>
</div>
<div class="col-sm-7">
<!-- <div id="cart" >-->
<h2 align="center">Items on Cart</h2>
<div>
<?php $cart_check = $this->cart->contents();
if(empty($cart_check)) {
echo 'To add products to your shopping cart click on "Add" Button';
} ?> </div>
<table id="table" border="0" cellpadding="5px" cellspacing="1px">
<?php
// All values of cart store in "$cart".
if ($cart = $this->cart->contents()): ?>
<tr id= "main_heading" class="well">
<td style="padding-left:15px;"><?>Name</td>
<td>Price(Rs)</td>
<td>Qty</td>
<td>Amount</td>
<td>Remove</td>
</tr>
<?php
// Create form and send all values in "shopping/update_cart" function.
echo form_open('shopping/update_cart');
$grand_total = 0;
$i = 1;
foreach ($cart as $item):
echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
echo form_hidden('cart[' . $item['id'] . '][rowid]', $item['rowid']);
echo form_hidden('cart[' . $item['id'] . '][name]', $item['name']);
echo form_hidden('cart[' . $item['id'] . '][price]', $item['price']);
echo form_hidden('cart[' . $item['id'] . '][qty]', $item['qty']);
?>
<tr class="well">
<td style="padding-left:15px;">
<?php echo $item['name']; ?>
</td>
<td>
<?php echo number_format($item['price'], 2); ?>
</td>
<td>
<?php echo form_input('cart[' . $item['id'] . '][qty]', $item['qty'], ' type="number" max="99" min="1" value="1" style="width:50px;"'); ?>
</td>
<?php $grand_total = $grand_total + $item['subtotal']; ?>
<td>
Rs <?php echo number_format($item['subtotal'], 2) ?>
</td>
<td>
<?php
// cancle image.
$path = "<img src='http://127.0.0.1/codeigniter_cart2/images/cart_cross.jpg' width='25px' height='20px'>";
echo anchor('shopping/remove/' . $item['rowid'], $path); ?>
</td>
<?php endforeach; ?>
</tr>
<tr>
<td style="padding-left:30px;"><b>Order Total: Rs <?php
//Grand Total.
echo number_format($grand_total, 2); ?></b></td>
<td colspan="5" align="right"><input type="button" class ='fg-button teal' value="Clear cart" onclick="window.location = 'shopping/remove/all'">
<?php //submit button. ?>
<input type="submit" class ='fg-button teal' value="Update Cart">
<?php echo form_close(); ?>
</td>
</tr>
<?php endif; ?>
</table>
</div>
<!-- <div id="products_e" align="center">-->
<!--</div>-->
<!-- </div>-->
</div>
</div>
</body>
Now my controller:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Shopping extends CI_Controller {
public function __construct()
{
parent::__construct();
//load model
$this->load->model('billing_model');
$this->load->library('cart');
}
public function index()
{
$data['products'] = $this->billing_model->get_all();
$this->load->view('shopping_views', $data);
}
function add()
{
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'qty' => 1
);
$this->cart->insert($insert_data);
redirect('shopping');
return TRUE;
}
function remove($rowid) {
// Check rowid value.
if ($rowid==="all"){
// Destroy data which store in session.
$this->cart->destroy();
}else{
// Destroy selected rowid in session.
$data = array(
'rowid' => $rowid,
'qty' => 0
);
// Update cart data, after cancle.
$this->cart->update($data);
}
// This will show cancle data in cart.
redirect('shopping');
}
function update_cart(){
// Recieve post values,calcute them and update
$cart_info = $_POST['cart'] ;
foreach( $cart_info as $id => $cart)
{
$rowid = $cart['rowid'];
$price = $cart['price'];
$amount = $price * $cart['qty'];
$qty = $cart['qty'];
$data = array(
'rowid' => $rowid,
'price' => $price,
'amount' => $amount,
'qty' => $qty
);
$this->cart->update($data);
}
redirect('shopping');
}
}
How should I apply this code to ajax?
Thanks for the help.
it's not necessary to use "success:"
However if you want to check error or debug
1) press F12 on Browser to open Developer Mode
2) do action (add cart)
3) on tab "Network" find your ajax request and see your error
Modify your add() function as follows:
function add(){
$insert_data = array('k1' => 'v1', 'k2' => 'v2');
$success = $this->cart->insert($insert_data);
if($success){
$res = array('status' => 200, 'msg' => 'success', 'somekey' => 'somevalue');
}else{
$res = array('status' => 500, 'msg' => 'database err');
}
echo json_encode($res);
}
Now the add() function has responded the ajax request with some json-formated data, you can parse the response data in javascript. The (success:) function should be like this:
function(response) {
if (response){
var res = JSON.parse(response);
if(res.status == 200){
window.location.href="http://127.0.0.1/codeigniter_cart2/index.php/shopping";
}else{
//error handler
alert(res.msg);
}
}else{
alert('sorry');
}
});
function __construct(){
parent::__construct();
$this->load->library(array('session','cart'));
$this->load->library('');
$this->load->helper('url');
$this->load->database();
}
public function product_list(){
$this->db->select('*');
$this->db->from('product');
$this->db->order_by('product_id','desc');
$rs = $this->db->get();
return $rs->result_array();
}
public function product_byId($pid){
$this->db->select('*');
$this->db->from('product');
$this->db->where('product_id',$pid);
$rs = $this->db->get();
return $rs->row_array();
}
Download full code from here http://phpcooker.com/codeigniter-shopping-cart

Cart is updated only once using Jquery and Codeigniter

I am working on Updating Codeigntier Cart with Jquery using Ajax call to update. Here is my jquery function
$(function() {
$('.cart_form select').on('change', function(ev) {
var rowid = $(this).attr('class');
var qty = $(this).val();
var postData_updatecart = {
'rowid' : rowid,
'qty' : qty
};
//posting data to update cart
$.ajax({
url : base_url + 'bookings/update_booking_cart',
type : 'post',
data : postData_updatecart,
beforeSend : function() {
$('#cart_content').html('Updating...');
},
success : function(html) {
//window.location.href = "postproperty.php?type="+suffix+'&page=page1';
$('#cart_content').html(html);
}
});
});
});
I have cart view file as
<div class="cart_form">
<?php echo form_open(base_url().'index.php/bookings/customerdetails', array('class' => 'bookings_form_customer', 'id' => 'bookings_form_customer')); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th style="text-align:left">Item Description</th>
<th style="text-align:left">QTY</th>
<th style="text-align:right">Item Price</th>
<th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td>
<select name="<?php echo $i.'[qty]'; ?>" class="<?php echo $items['rowid']; ?>">
<?php
for($tt=0; $tt<=10; $tt++)
{
?>
<option value="<?php echo $tt; ?>" <?php if($tt==$items['qty']) echo 'selected="selected"'; ?>><?php echo $tt; ?></option>
<?php
}
?>
</select>
</td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"> </td>
<td class="right"><strong>Total</strong></td>
<td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<p><?php echo form_submit('update_cart', 'Update your Cart'); ?></p>
</form>
</div>
Things are working Nice. But, lets say if I have 2 products, if i select one item's qty from 1 to 2, cart is updated and updated cart is shown using AJAX. But, if I immediately change another item's qty then it doesnt work.
The cart view file is inside class 'cart_form'.
Helping hands are appreciated.
I got an answer. You can refer below link
Ajax Request works only once
OR
Directly follow this
REPLACE jquery code part by
$(function() {
$(document).on( "change", "#bookings_form_customer select", function(ev) {
// your code goes here
});
});

Categories