I am trying to update a value when data = success but is its giving an error Jquery/PHP
I am trying to update a session called $_SESSION['counting']
But its not working, i made this based on what i learned from a user here.
look my code.
index.php i have this....
as you can see my $result['items']=$_SESSION['counting'];
$result['items'] is defined.
but i get an error in my console saying : Uncaught ReferenceError: data is not defined(…)
When i paste this $('.total_items').html(data.items); in console.
Why is that happening?
Good news, i fix this problem.
What i did was,i created a new file cart_function2.php
And i took off all the code,i let only my add code.
And it worked fine.
I am trying to update a session called $_SESSION['counting']
In your HTML you are simply echoing the value that is stored in $_SESSION['counting']. You are not updating it.
You have at least two invalid element attributes -- aria-hidden and product_id. Are you using a custom doctype DTD?
The following line is confusing because action is a variable, but it is being used as an object key. I have a feeling that jQuery doesn't have problem with this but I would consider it a bad practice. (Line 11 from your script in index.php)
I do not see a result key/value pair in the $result array that is JSON encoded and returned by cart_functions.php. So unless you have omitted that part, the following should generate an error because data.result would not exist.
Please post what is being displayed in the console. I have a feeling that cart_functions.php is not returning what you think it is. The error suggests that the returned data does not exist.
Is this page live so that we can see the ajax conversation?
Brian TaoSenior Business Solutions ConsultantCommented:
Uncaught ReferenceError: data is not defined(…)
This only happens when you paste this $('.total_items').html(data.items); in console, but no error when your ajax call returns, right?
To answer your question
Why is that happening?
:
Because you're out of scope. data is only available in the done(function(data){}) event handler but you're trying to refer to it in the console which is at a global (window) level.
If the "total_items" span doesn't get updated, then we'll have to see the whole picture.
0
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
1. this script i reused from my checkout page,where i have 3 buttons where the function for it, is in cart_functions.php.
this is the full code for it.
2. And in my checkout.php i have this script that works with and include from cart.php.
My cart.php i have one function,that is displaying all products in session.
so in my checkout i have <?php cart(); ?> that this function comes from cart.php
(this script is responsable for my drop downs to send data and get result of costs of product)
calculates.php code here
so my jquery script on checkout is..
$(document).ready(function() { $('.fabric, .size').on('change', sendData); function sendData() { //use the data-id attribute of the selected element to match the correct elements var id = $(this).data("id"); var action = $(this).attr('data-action'); var fabricID = $('.fabric[data-id=' + id +']').val(); var sizeID = $('.size[data-id=' + id +']').val(); if ( fabricID !== "" && sizeID !== "") { $.ajax({ type : 'GET', url : 'calculates.php', dataType : 'json', data : { prod_id:id, fabric_id: fabricID, action:action, size_id: sizeID } }).done(function(data) { console.log(data); $('.cost[data-id=' + id + ']').html('R$:' + data.cost); $('.subtotal[data-id=' + id + ']').html('R$:' + data.subtotal); }); } }});
this function is included in my index.php
this is the button that sends a product to checkout.
So i am trying to do, is when these button above sends a product to checkout, it will be counted in my span tag in the beginning of the question i made.
Sorry for this big information, but now you will understand what im doing
OK. I misunderstood the question. I think Brian Tao has answered why you're getting the error. But I also think your ultimate issue is the fact that $_SESSION['counting'] is not being updated on your page. Correct?
Your actions buttons are using AJAX to send an item to the back end which is changing the value of $_SESSION['counting']. Correct? But the value displayed in your <span> tag is not changing. Correct?
When the page loads, the current value of $_SESSION['counting'] is echoed in the page source code. There is no link after that point between the $_SESSION['counting'] variable on the back end and the value displayed in the <span> tag. Nor is there a link from the page being displayed in the browser to the $_SESSION['counting'] variable on the back end. When the page is loaded or refreshed, the back end $_SESSION array is uploaded to the front end and stored in a cookie. It is not dynamic.
Your ajax .done() clause would have to update the text in the span. It could do this from the data.items value returned from the ajax call. (See line 18 below)
$('.actions').click(function(e){ e.preventDefault(); var action = $(this).attr('data-action'); //identifica a ação do botão var id = $(this).attr('product_id'); var cost_id = $(this).data("id"); console.log("triggered action " + action + " for product " + id); //debugging $.ajax({ url: 'cart_functions.php', type : 'GET', data: {action:action,prod_id:id}, dataType: 'json' // Be consistent and use .done()}).done(function(data) { if (data.result == "success") { $(".product" + id).text(data.val); $('.subtotal[data-id=' + id + ']').html('R$:' + data.subtotal); $(".total_items").text(data.items); } else { alert("error"); }}).fail(function(jqXHR, textStatus, errorThrown) { //in case of server/network error console.log("An error occurred whilst attempting to contact the server: " + jqXHR.status + " " + textStatus + " " + errorThrown);});});
Like sense my add to cart button is sending data by ajax to cart_functions.php, and i created the $result['items'].
I tought that when i click on add to cart, my script in index should grab the counted session product,and send the data to $result['items'];
where on success should by updated in my span tag.
Do you have a tip to how can i make this better so it works?
Do you have multiple elements with the class "total_items"? It may only be changing the first one. Can you post a link to the live page? Otherwise, can you give the span a unique id that we can target?
what is this line $_SESSION[$prodname.'icms']; doing in your function add_prod($prod, $prodname)? I don't see such key being used in anywhere else.
what is line #9 $result; doing?
what is line #36 $result['totals'] = new stdClass; doing?
what is this line unset($_SESSION['icms'.$prod]); doing in your function delete_prod($prod, $prodname)? I don't see it being set in anywhere in your script.
And 1 more thing to try:
change the following line
Brian TaoSenior Business Solutions ConsultantCommented:
Ok, when you changed it to 999 it changes the screen display, but nothing else worked, right?
Apparently it means neither of your $_SESSION['counting'] or the new $_SESSION['products'] is working in the cart_functions.php.
Can you please attach your files here and not just copy&paste part of your code? Sometimes it's the hidden part that's causing the problem.
And, you haven't answer my questions in the same comment 41847993
My cart_functions.php
the cart_functions is responsible for making my buttons add, plus, minus and remove to work.
And when these buttons are clicked my cost,and sub-total changes.
This is all working fine =D
My cart.php
In cart.php i have only a function that gives me data and dropdown options for each product.
My calculates.php
calculates.php is responsible for giving the cost based on fabric and size options from the dropdowns.
My checkout.php
Is where everything works with script above.
My index.php
In here is my product gallery, and my product counter
Good news, i fix this problem.
What i did was,i created a new file cart_function2.php
And i took off all the code,i let only my add code.
And it worked fine.
this is how my code looks like now:
Open in new window