简体   繁体   中英

Adding a item into a cart automatically using javascript

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM