1

I have created some dynamic text boxes and values according to the no. of cart items selected by a user. The no. of cart items are stored in a cookie and I am trying to do the following:

1) Get the no. of cart items from the cookie named cartcounter
2) Create 'cartcounter' no of text boxes with name itemkey_skey where itemkey is the hash key of each items in user cart.
3) Submit the form
4) Get the keys of the selected items in php

Now I can create cartcounter, the number of text boxes with different values.

But the problem is that I can get only the first text box value in $_POST in PHP. I would like to get all the values of the dynamically created form elements.

Code:

$(document).ready(function() {
    if (getCookie("cartcounter") != 0) {
        $("#counter").val(getCookie("cartcounter"));
        for (var i = 1; i <= cartcounter; i++) {

            var ckey = getCookie(i + "_skey");
            var element = document.createElement("input");
            alert("Element=" + element);
            element.setAttribute("type", "text");
            element.setAttribute("value", ckey);
            element.setAttribute("name", "skey_" + ckey);
            var foo = document.getElementById("itmbox");
            foo.appendChild(element);
            $("#frm").submit();
        }
    }
    else {
        alert('Your cart is empty , please choose your product');
        window.location = "http://synergiadigital.com/restaurant2/orderFood.php/"
    }
});​
1
  • Is there a reason your not using a session or accessing the cookie with php? Commented Sep 17, 2012 at 4:38

1 Answer 1

1

I have solved it. The problem was that I was calling the submit function inside the loop. I have solved it by doing:

$(document).ready(function() {
    if (getCookie("cartcounter") != 0) {
        $("#counter").val(getCookie("cartcounter"));
        for (var i = 1; i <= cartcounter; i++) {

            var ckey = getCookie(i + "_skey");
            var element = document.createElement("input");
            alert("Element=" + element);
            element.setAttribute("type", "text");
            element.setAttribute("value", ckey);
            element.setAttribute("name", "skey_" + ckey);
            var foo = document.getElementById("itmbox");
            foo.appendChild(element);

        }
        $("#frm").submit();
    }
    else {
        alert('Your cart is empty , please choose your product');
        window.location = "http://synergiadigital.com/restaurant2/orderFood.php/"
    }
});​
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.