0

Hi i have a cart that i want to add a form automatically this code seems to kinda do the trick only the problem is i have to hit F5 for it to add the amount i am quite new to this and cant figure out where i have gone wrong.

<form method="post" action="level3.php" class="jcart" id="foo">
            <fieldset>
                    <input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
                    <input type="hidden" name="my-item-id" value="ABC-8" />
                    <input type="hidden" name="my-item-name" value="Level 1" />
                    <input type="hidden" name="my-item-price" value="95.00" />
                    <input type="hidden" name="my-item-url" value="" />

                    <table>
                    <tr>
                    <td width="65%">
                        <strong>Level 1 all for £95</strong>
                    </td>
                    <td width="15%" align="right">
                        £95.00
                    </td>

                    <td>
                            <input type="text" hidden ="true" name="my-item-qty" value="1" size="3" hidden="true" />
                    </td>
                    <td width="20%" align="center">
        <input type="submit" name="my-add-button" id="my-add-button" value="add" class="button" /> 
                    </td>
                    </tr>
                    </table> 
                    <input type="hidden" name="visited" value="" />       
                </fieldset>

That is the form that submits the amount into the check out.

<script>
    $(document).ready(function(){

        //Check if the current URL contains '#' 
        if(document.URL.indexOf("#")==-1)
        {
            url = document.URL+"#";
               location = "#";   

        } else {
            location.reload(true);
               document.getElementById("my-add-button").click();// Simulates button click this has to be in as it links to another piece of java that adds the item to the check out with out this it wont add the item
                document.foo.submit(); // Submits the form without the button   
        }

    });

    </script>

document.getElementById("my-add-button").click();

The code above links to the code below that adds the items from to my knowledge

$('.jcart').submit(function(e) {
        add($(this));
        e.preventDefault();
    });

Thank you in advance for any help or suggestions

2
  • Any HTML or some description what functions you are using? What framework you are using?
    – Justinas
    Jun 2, 2014 at 8:50
  • @Justinas I have edited the question to give more details if that helps. Jun 2, 2014 at 9:11

1 Answer 1

0

If i understand correctly, you increase some value of cart in PHP and than want to update quantity on page without refreshing page.

I made little JSFiddle: http://jsfiddle.net/7SbBA/

Basically use ajax:

$.ajax({
  url: 'example.com',
  data: /* pass product id here */
  success: function(){
     /* update quantity you want */
  }
}); 

There is some html change too.

UPDATE

If you want updated total values on page load, just £<span class='amount'><?php echo $product['quantity'] * $product['price'] ?></span> and there is no need to use jquery/js for that. But if you still want dynamically update on window load, i updated my JSFiddle v.2

1
  • hi that adds to the cart but i would like it to do it automatically as you land on the page. if that is possible thank you for all your help :) i am carrying on working on it so if i get any further with your method ill let you know :) Jun 2, 2014 at 10:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.