0

I am trying to build a shopping cart application. I have all the items in shopping cart inside a javascript object called Cart. Data in Cart is of the form {"sku"=quantity} For example.

     Cart={"5x123"=1,"5x125"=3}

Now I have a form which accepts shipping and billing address.

 <form method="post" action="/perl/xxxx/echo.cgi"> 
    <table>
    <tr>
        <td>Name:</td>
        <td><input type="text" name="name" size="30" /></td>
    </tr>

    <tr>
        <td>Shipping Address:</td>
        <td><Input type="text" name="address" id="saddress" size="40" /></td>
    </tr>
    ....
    <tr>
        <td><input type="reset" /></td>
        <td><input type="submit" value="Submit Order"/></td>
    </tr>  
    </table>
   </form>   

When I submit this form I need to store "sku" and "quantity" from Javascript Object Cart into DB using perl.

Could someone please tell me the easiest way to do this?

Should I ajax or hidden values in form?

If I use ajax, then how to retrieve values from Cart inside perlscript?

If I use hidden values this way

    <input type=hidden name=sku value="">
     //How to populate value field
    //I need to have as many hidden fields as the number of items in cart 

Thanks,

2 Answers 2

1

Use AJAX in the browser to submit your JavaScript object to the server. The Jquery javascript library is excellent for helping you here.

Then, use the CGI perl module in your cgi to retrieve the posted values, and the JSON module (as previously mentioned) to transform the JSON object to a perl hash.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I am pretty new to AJAX and JQuery. Here is the AJAX call I created. $(':submit').on('click',function(e){ if(!validate_fields_on_submit()) { e.preventDefault(); return; } $.ajax({ dataType : 'text', // the datatype you are returning url: '/perl/xxxx/store.cgi', data:Cart, success:function (msg){ // data is the data returned from the ajax call handle.innerHTML = msg; } }); Is this right?
difficult to check syntax when it's in a comment like that, but the parts all seem to be there.
0

I suspect ajax is the way to go. To read your Cart json object in Perl, use the JSON module, specifically decode_json. It'll convert your data into a perl-friendly hash, which you can then reference as needed.

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.