Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
This code works perfectly for the 1st time but fails for the second time. I have tried cache: false but it didn't work.
How can I do this?
Thanks,
Akkywadhwa

JavaScript
function onQuantityChange(id)
{
    var quantity=$( "#"+id+" option:selected" ).text();

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "cart.php?id="+id+"&quantity="+quantity,
        success: function(data)
        {
            $('.success, .warning, .attention, .information, .error').remove();
            if(data.HTML!=null)
           {     
		//code to update the view on page
           }
               
            
            else
            {
                alert("Unable to get it");
            }
        },
        error: function(){
        alert("Error in event:onselect");
        }
    });
}


PHP
for($i=0;$i<count($_SESSION["contents"]);$i++)
{
$sql="select * from products where ID=".$_SESSION["contents"][$i]["ItemCode"];
$con->Query($sql);
while($row=$con->Row())
{
echo "<tr>";
echo "<td class=\"name\">";
echo "<a href=\"\">".$row->Name." ".$row->Unit."</a>";
echo "</td>";
echo "<td class=\"details\"><span class=\"MRP\">Rs $row->MRP</span></td><td class=\"details\"><span class=\"details\">Rs $row->SP</span>";
echo "</td>";
echo "</td>";
echo "<td class=\"quantity\">";
echo "<select id=\"$row->ID\" onchange=\"onQuantityChange('$row->ID')\" >";
for($j=1;$j<21;$j++)
{
if($j==$_SESSION["contents"][$i]["quantity"])
{        
echo "<option value=\"$j\" selected>$j</option>";
}
else
{
echo "<option value=$j>$j</option>";    
}
}
echo "</select>";
echo "</td>";
echo "<td id=\"$row->ID\" class=\"total\">".$_SESSION["contents"][$i]["quantity"]*$row->SP."</td>";
echo "<td class=\"remove\">";
echo "<img src=\"Stylesheets/image/remove.png\" >ID')\" title=\"Remove\"/>";
echo "</td>";
echo "</tr>";
}
}
}
else
{
echo "contents array doesnt exists.";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
echo "<div class=\"cart-totals\">";
    echo "<table class=\"right\">";              
                echo "<tr>";
              echo "<td class=\"total\" width=\"200px\" align=\"right\">";
               echo "<h3>Shipping</h3>";
               echo "</td>";
               echo "<td class=\"total\" width=\"70px\" align=\"right\">";
               echo "Free";
               echo "</td>";
               echo "</tr>";
               echo "<tr>";
               echo "<td class=\"total\" width=\"200px\" align=\"right\">";
               echo "<h3>Total</h3>";
               echo "</td>";
               echo "<td class=\"total\" width=\"70px\" align=\"right\">";
               echo "<h3 id=\"FinalTotal\">Rs ".$_SESSION["total"]."</h3>";
               echo "</td>";
               echo "<td class=\"total\" width=\"40px\">";
               echo "</td>";
               echo "</tr>";
               echo "</table>";
echo "</div>";
echo "</div>";

echo "<div id=\"BackForthButtons\">";
    echo "<div class=\"checkout\">";
        echo "<div class=\"cart-modules left\">";
            echo "</div></div><div class=\"right\"><a class=\"button\" href=\"checkout.php\">";
        echo "<span class=\"proceed\">Checkout</span></a></div>";
echo "<div class=\"right\">";
echo "<a class=\"button\" href=\"http://www.cartup.in\" style=\"background: #454545; margin: 2px 5px;\">";
echo "<span class=\"continue\">Add more items</span>";
echo "</a>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
Posted
Updated 25-Jul-19 9:31am
Comments
ZurdoDev 11-Jan-15 22:03pm    
What do you mean it fails?
Akkywadhwa 12-Jan-15 0:29am    
I want to execute the above ajax on a particular event, say selection changed event of a combo box. This ajax works when event is fired for the first time but doesn't works for subsequent firing of same event.
Sinisa Hajnal 12-Jan-15 2:44am    
How it doesn't work? It never calls the method? It shows one of your error messages? Which one? You have to be precise, we cannot see your screen.
ZurdoDev 12-Jan-15 7:29am    
Debug the code and see where it gets. As Sinisa said, you have to tell us specifics. We can't see what's happening. "Doesn't work" means nothing to us.
Akkywadhwa 12-Jan-15 15:36pm    
This code is used to update a <div> on the webpage. The AJAX request fetches the data from the URL. From "Doesn't Work" I want to say that this piece of code doesn't update that particular <div>

hello Akkywadhwa

the problem is that your call is cached.
this is because your using the get method.
You appended your values to the end of the url.

a simple solution is to add a random value at the end.

so:

var rand = Math.rand();

url: "cart.php?id="+id+"&quantity="+quantity + "&rand=" + rand,

You then still use the get method.

rather use the post method:

var data = { "id" : id,
"quantity" : quantity };


$.ajax({
type: "POST",
dataType: "json",
url: "cart.php",
data: data,

....

succes.
 
Share this answer
 
Comments
Akkywadhwa 12-Jan-15 15:01pm    
Nopes!
None of the method in the given solution works.
Akky
Akkywadhwa 12-Jan-15 15:32pm    
I have tried different selectors too.
Example:
$(document).on("change","some_class",function(){ });
hello, i use post method, but i have same problem.
 
Share this answer
 
Comments
Patrice T 25-Jul-19 16:58pm    
Open a new question and give proper details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900