I want when I add a product to the cart and reload the same page, but the problem did not this product.
The controller
public function detail()
{
$data=array('title' =>'Ecommerce Online | Detail Product',
'username' => $this->session->userdata('id'),
'categories' => $this->categories_model->get_categories(),
'details' => $this->categories_model->get_details($this->uri->segment(3)),
'isi' =>'member/detail');
$this->load->view('template_home/wrapper',$data);
}
function addtocart()
{
if($this->cart_model->validate_add_cart_item() == TRUE){
if($this->input->post('ajax') != '1'){
redirect('member/detail/'); // this problem
}else{
echo 'true';
}
}
}
I add my models
function validate_add_cart_item()
{
$id = $this->input->post('product_id');
$cty = $this->input->post('quantity');
$this->db->where('productID', $id);
$query = $this->db->get('product', 1);
if($query->num_rows > 0){
foreach ($query->result() as $row)
{
$data = array(
'id' => $id,
'qty' => $cty,
'price' => $row->price,
'name' => $row->productName
);
$this->cart->insert($data);
return TRUE;
}
}else{
return FALSE;
}
}
I add my view
<?php foreach ($details as $s) { ?>
<div class="col-md-5">
<div class="box text-center">
<img src="<?php echo base_url('upload/'.$s->photo); ?>" width="150px" height="150px">
<br><?php echo $s->productName; ?>
<br><strong>Rp. <?php echo $s->price; ?></strong>
<br>
<?php echo form_open('member/add'); ?>
<fieldset>
<label>Quantity</label>
<?php echo form_input('quantity', '1', 'maxlength="2"'); ?>
<?php echo form_hidden('product_id', $s->productID); ?>
<?php echo form_submit('add', 'Add'); ?>
</fieldset>
<?php echo form_close(); ?>
</div>
</div>
<?php } ?>
Jquery script
<script type="text/javascript">
$(document).ready(function() {
/*place jQuery actions here*/
var link = "<?php echo site_url('member/detail')?>/"; // Url to your application (including index.php/)
$(".detail-product").submit(function(){
var id = $(this).find('input[name=product_id]').val();
var qty = $(this).find('input[name=quantity]').val();
$.post(link + "member/add", { product_id: id, quantity: qty, ajax: '1' },
function(data){
if(data == 'true'){
$.get(link + "member/detail", function(cart){ // Get the contents of the url cart/show_cart
$("#cart_content").html(cart); // Replace the information in the div #cart_content with the retrieved data
});
}else{
alert("Product does not exist");
});
return false; // Stop the browser of loading the page defined
});
});
</script>
This is problem url: http://localhost/scientificwriting/member/detail/ and productid can not be invoked. Do I need to replace the IF statement on my controller and my jquery?
Please help me thanks
Related
Actually my data is getting stored in backend but I'm not able to display it in frontend , also i want to add multiple images in post type wordpress plugin
You can follow the code:
//Add meta box in the post or page
add_action( 'add_meta_boxes', 'custom_postpage_meta_box' );
function custom_postpage_meta_box(){
$post_types = array('post');
foreach($post_types as $pt){
add_meta_box('custom_postpage_meta_box',__( 'More Featured Images', 'textdomain'),'custom_postpage_meta_box_func',$pt,'side','low');
}
}
//Load the field in meta screen
function custom_postpage_meta_box_func($post){
$meta_keys = array('second_featured_image');
foreach($meta_keys as $meta_key){
$image_meta_val=get_post_meta( $post->ID, $meta_key, true);
?>
<div class="custom_postpage_wrapper" id="<?php echo $meta_key; ?>_wrapper" style="margin-bottom:20px;">
<img onclick="custom_postpage_add_image('<?php echo $meta_key; ?>');" src="<?php%20echo%20(%24image_meta_val!=''?wp_get_attachment_image_src(%20%24image_meta_val)%5B0%5D:'');%20?>" style="width:100%;cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" alt="">
<a class="addimage" style="cursor:pointer;" onclick="custom_postpage_add_image('<?php echo $meta_key; ?>');"><?php _e('Set featured image','textdomain'); ?></a><br>
<a class="removeimage" style="cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" onclick="custom_postpage_remove_image('<?php echo $meta_key; ?>');"><?php _e('Remove featured image','textdomain'); ?></a>
<input type="hidden" name="<?php echo $meta_key; ?>" id="<?php echo $meta_key; ?>" value="<?php echo $image_meta_val; ?>">
</div>
<?php } ?> <script>function custom_postpage_add_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
custom_postimage_uploader = wp.media.frames.file_frame = wp.media({
title: '<?php _e('select image','textdomain'); ?>',
button: {
text: '<?php _e('select image','textdomain'); ?>'
},
multiple: false
});
custom_postpage_uploader.on('select', function() {
var attachment = custom_postpage_uploader.state().get('selection').first().toJSON();
var img_url = attachment['url'];
var img_id = attachment['id'];
$wrapper.find('input#'+key).val(img_id);
$wrapper.find('img').attr('src',img_url);
$wrapper.find('img').show();
$wrapper.find('a.removeimage').show();
});
custom_postpage_uploader.on('open', function(){
var selection = custom_postpage_uploader.state().get('selection');
var selected = $wrapper.find('input#'+key).val();
if(selected){
selection.add(wp.media.attachment(selected));
}
});
custom_postpage_uploader.open();
return false;
}
function custom_postpage_remove_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
$wrapper.find('input#'+key).val('');
$wrapper.find('img').hide();
$wrapper.find('a.removeimage').hide();
return false;
}</script> <?php wp_nonce_field( 'custom_postpage_meta_box', 'custom_postpage_meta_box_nonce' );
?>
}
//Save the meta box
add_action( 'save_post', 'custom_postimage_meta_box_save' );
function custom_postimage_meta_box_save($post_id){
if ( ! current_user_can( 'edit_posts', $post_id ) ){ return 'You have no permission to edit this post types'; }
if (isset( $_POST['custom_postimage_meta_box_nonce'] ) && wp_verify_nonce($_POST['custom_postimage_meta_box_nonce'],'custom_postimage_meta_box' )){
$meta_keys = array('second_featured_image');
foreach($meta_keys as $meta_key){
if(isset($_POST[$meta_key]) && intval($_POST[$meta_key])!=''){
update_post_meta( $post_id, $meta_key, intval($_POST[$meta_key]));
}else{
update_post_meta( $post_id, $meta_key, '');
}
}
}
}
For more details please read the article https://yourblogcoach.com/upload-multiple-featured-images-in-a-post-or-page/
Thank you
I am using Ajax for my dropdown to select Province and City.
I wanted the City to be populated after I choose Province.
I already have my code but unfortunately it doesn't work.
Here are my codes
javascript
<script type="text/javascript">
function provChange(){
var province = $("#province_id").val();
$.ajax({
url: "<?php //echo $this->Html->url('/front/ajax_city_id'); ?>/"+province+"/<?php //echo $this->request->data('Car.city_id'); ?>",cache: false,
success: function(msg){$("#city_id").html(msg); },
"statusCode": {
403: function() {
window.location.href="<?php //echo $this->Html->url(array('controller'=>'front','action'=>'index')); ?>"
},
500: function() {
alert('Error Server Side occured');
}
}
});
}
</script>
view.ctp
<div class="emBeli form large-9 medium-8 columns content">
<?php echo $this->Form->input('provinsi',array('type'=>'select','empty'=>'Provinsi','options'=>$provinsis,'id'=>'province_id','class'=>'form-control form-control-custom','label'=>false,'onChange'=>'provChange()')); ?>
<?php echo $this->Form->input('kota',array('type'=>'select','empty'=>'Kota','options'=>$kota,'class'=>'form-control form-control-custom','label'=>false)); ?>
</div>
ajax_city_id.ctp
<?php
if ($key==null) echo "<option value=\"\">Kota</option>";
foreach($types as $i):
if($i['Kota']['id']==$key) echo "<option value=\"".$i['Kota']['id']."\" selected>".$i['Kota']['name']."</option>";
else echo "<option value=\"".$i['Kota']['id']."\">".$i['Kota']['name']."</option>";
endforeach;
?>
controller.php
public function ajax_city_id($id = null, $key = null, $cur = null)
{
$this->layout = 'ajax';
$this->set('types', $this->Kota->find('all', array('conditions' => array('display' => 1, 'Kota.province_id =' => $id), 'order' => array('Kota.name' => 'asc'))));
$this->set('key', $key);
}
all i got is ReferenceError: Can't find variable: $
which means it can't find variable $("#province_id") on my javascript code.
please somebody help......
If I call the .php file directly it works great, so I believe the error is in how I am displaying the AJAX result. I am new to AJAX. I have a form that the user can select dates to search on, etc and I would like to results to be displayed when returned.
$("#searchForm").on("submit", function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "searchnow.php",
data: $('#searchForm').serialize(),
success : function (output) {
console.log(output);
}
});
});
searchnow.php
<div id="output">
<div class="container-fluid">
<div class='features'>
<?php
include ("config.php");
$con = mysql_connect($server, $user, $password) or die('Sorry, could not connect to database server');
mysql_select_db($database, $con) or die('Sorry, could not connect to database');
$query = "SELECT * FROM hsevents WHERE
CURDATE()<=enddate AND
verified='Y'
ORDER BY location";
$result = mysql_query($query) or die('Sorry, could not access the database at this time ');
$SPACES = "</br><b>Description:</b> ";
if (mysql_num_rows($result) == 0)
{
echo "<h3>Sorry, there are no classes or events that meet your criteria.</h3>";
}
else
{
$i = 1;
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
$unique = 'unique' . $i;
$tempname=htmlentities($row[name]);
$tempcontact=htmlentities($row[contact]);
$tempbegdate = date("m-d-Y", strtotime($row[startdate]));
$tempenddate = date("m-d-Y", strtotime($row[enddate]));
ob_start();
if ( ($i%4) === 0) {
echo "<div class='row row-padded row-bordered row-centered'>";
}
echo "
<div class='col-md-3'>
<div class='panel'>
<div class='panel-heading'>
<img src='images/$row[category]' class='img-responsive img-thumbnail img-hsb'>
</div>
<div class='panel-body text-center'>
$tempname</br>
Start Date: $tempbegdate</br>
End Date: $tempenddate</br>
Location: $row[location]</br>
Age Range: $row[lowerage] - $row[upperage]</br>
Contact: <a href=$tempcontact>$tempcontact</a></br>
<div id='$unique' class='collapse collapse-hsb'>
$tempdescription
</div>
</div>
</div>
</div>
";
if ( ($i%4) === 0) {
echo "</div>";
}
$classdata = ob_get_contents();
ob_end_clean();
?>
<?php echo $classdata ?>
<?php
$i++;
}
$classdata = ob_get_contents();
ob_end_clean();
echo $classdata;
}
?>
</div>
</div>
</div>
You just need to provide the DIV or any Selector inside the success function, to replace the DIV or selector html with the AJAX success output. Here i used #MyDivId which should be your html element in which you need to print the AJAX output.
$("#searchForm").on("submit", function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "searchnow.php",
data: $('#searchForm').serialize(),
success : function (output) {
output = output.trim(); // Trim's unwanted white space around the output
if (output != "") {
$("#MyDivId").html(output); // This Adds the AJAX output inside #MyDivId
}
}
});
});
Please help me to correct the code. Where it is conflicting with other javascript action. This price filter in opencart is stopping the action of Add to cart in category page. following is the code where php is called inside the javascript. Something is missing or not properly coded.
<div class="row">
<div class="slide-filter">
<h2><?php echo $heading_title; ?></h2>
<div class="list-group">
<div class="list-group-item">
<div id="filter-group1">
<div id="scale-slider"></div>
<button type="button" id="button-price-filter" class="filter-button"><?php echo $button_filter; ?></button>
</div>
</div>
</div>
</div>
</div>
and the javascript is
$("#scale-slider")
.slider({
min: <?php echo round($price_range_min) ; ?>,
max: <?php echo round($price_range_max) ; ?>,
range: true,
values: [<?php echo (isset($price_range[0])?$price_range[0]:0); ?>, <?php echo (isset($price_range[1])?$price_range[1]:$price_range_max); ?>]
})
.slider("pips", {
rest: false,
<?php if (!$right_code) { ?>
prefix: "<?php echo $price_code; ?>",
<?php } else { ?>
suffix: "<?php echo $price_code; ?>"
<?php } ?>
})
.slider("float");
(function( $ ) {
$(function() {
$('#button-price-filter').on('click', function() {
priceRange = [];
$('#scale-slider .ui-slider-tip').each(function(){
priceRange.push($(this).html());
});
$('.<?php echo $product_class; ?>').hide();
$('.clearfix').remove();
$('.<?php echo $product_class; ?>').each(function(){
if( $(this).find( ".price span.price-new" ).length ) {
var price = $(this).find( ".price span.price-new" ).html().replace('<?php echo $price_code; ?>','').replace(',','');
} else {
var text = $(this).find('.price').html().replace('<?php echo $price_code; ?>','');
if( $(this).find( ".price span" ).length ) {
var price = text.substring(0,text.indexOf('<span')).replace(',','');
} else {
var price = text.replace(',','');
}
}
price = parseInt(price);
if( !isNaN(price) && (price > priceRange[0] && price < priceRange[1]) ){
$(this).fadeIn("slow");
}
});
});
});
})(jQuery);
remove the $('.clearfix').remove(); ,may be work perfectly
After doing a clean install of Magento Community Edition (v. 1.7.0.2) in my local machine and loading it with sample data provided by Magento, I can browse reviews, add items to cart, etc., as expected.
However, when I follow the same steps on a live server, JS functionality does not work, neither in Chrome nor in Firefox (I have not tried other browsers). For instance, clicking on 'Add to Cart' on a product page gives me nothing but a console error ("Uncaught ReferenceError: productAddToCartForm is not defined").
What is causing this behaviour, and how can I fix it?
I'm not sure if this will help you, but it fixed my problem. What I did was I moved the javascript segment in view.phtml from under "the add-to-cart-wrapper" class to above it.
Like so:
<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
try {
this.form.submit();
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
if (button && button != 'undefined') {
button.disabled = true;
}
}
}.bind(productAddToCartForm);
productAddToCartForm.submitLight = function(button, url){
if(this.validator) {
var nv = Validation.methods;
delete Validation.methods['required-entry'];
delete Validation.methods['validate-one-required'];
delete Validation.methods['validate-one-required-by-name'];
// Remove custom datetime validators
for (var methodName in Validation.methods) {
if (methodName.match(/^validate-datetime-.*/i)) {
delete Validation.methods[methodName];
}
}
if (this.validator.validate()) {
if (url) {
this.form.action = url;
}
this.form.submit();
}
Object.extend(Validation.methods, nv);
}
}.bind(productAddToCartForm);
//]]>
</script>
<div class="add-to-cart-wrapper">
<?php echo $this->getChildHtml('product_type_data') ?>
<?php echo $this->getChildHtml('extrahint') ?>
<?php if (!$this->hasOptions()):?>
<div class="add-to-box">
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
<span class="or"><?php echo $this->__('OR') ?></span>
<?php endif; ?>
<?php endif; ?>
<?php echo $this->getChildHtml('addto') ?>
<?php echo $this->getChildHtml('sharing') ?>
</div>
<?php echo $this->getChildHtml('extra_buttons') ?>
<?php elseif (!$_product->isSaleable()): ?>
<div class="add-to-box">
<?php echo $this->getChildHtml('addto') ?>
<?php echo $this->getChildHtml('sharing') ?>
</div>
<?php endif; ?>
</div>
Instead of:
<div class="add-to-cart-wrapper">
<?php echo $this->getChildHtml('product_type_data') ?>
<?php echo $this->getChildHtml('extrahint') ?>
<?php if (!$this->hasOptions()):?>
<div class="add-to-box">
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
<span class="or"><?php echo $this->__('OR') ?></span>
<?php endif; ?>
<?php endif; ?>
<?php echo $this->getChildHtml('addto') ?>
<?php echo $this->getChildHtml('sharing') ?>
</div>
<?php echo $this->getChildHtml('extra_buttons') ?>
<?php elseif (!$_product->isSaleable()): ?>
<div class="add-to-box">
<?php echo $this->getChildHtml('addto') ?>
<?php echo $this->getChildHtml('sharing') ?>
</div>
<?php endif; ?>
</div>
<?php echo $this->getChildHtml('related_products') ?>
<div class="clearer"></div>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container2', '', true, true) ?>
<?php endif;?>
</form>
<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
try {
this.form.submit();
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
if (button && button != 'undefined') {
button.disabled = true;
}
}
}.bind(productAddToCartForm);
productAddToCartForm.submitLight = function(button, url){
if(this.validator) {
var nv = Validation.methods;
delete Validation.methods['required-entry'];
delete Validation.methods['validate-one-required'];
delete Validation.methods['validate-one-required-by-name'];
// Remove custom datetime validators
for (var methodName in Validation.methods) {
if (methodName.match(/^validate-datetime-.*/i)) {
delete Validation.methods[methodName];
}
}
if (this.validator.validate()) {
if (url) {
this.form.action = url;
}
this.form.submit();
}
Object.extend(Validation.methods, nv);
}
}.bind(productAddToCartForm);
//]]>
</script>
</div>
I hoped this helped.
A colleague fixed this issue. There were two problems that were causing magento to fail on the live server:
Missing dependencies for PHP (dom and gm) which Magento was not complaining about.
A low memory limit in PHP. We needed to increase the memory_limit setting in /etc/php.ini (512M worked for us).