dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Send data but not in a form

  1. #1
    Join Date
    Jan 2010
    Posts
    7

    Question Send data but not in a form

    Hello,

    I am new to web development, and I am writing a little test site to learn some stuff. But I'm not sure how to send data to the server that is not in a form. I have two specific questions at the end of this post, but I'll try to give the relevant details first:

    I enter a url in my browser that maps to a JSP on the server (JBoss/Tomcat on the server side). That JSP reads products from a database and presents a little 'shop' on the browser.

    When the user adds an item to their cart, I make an Ajax call to the server, which adds to the cart and returns xml representing the cart contents.

    There is an image on the web page that takes the user to the shopping cart page where they can submit an order. When they click the "checkout" image, the follow Javascript is called:

    var req = new XMLHttpRequest();
    req.open("GET", "checkout", false);
    req.send(null);

    if (req.status == 200)
    {
    var doc = req.responseText;
    document.write(doc);
    }

    The checkout servlet writes html which contains a form for the user to enter their delivery details. As you will see, I wait for the servlet to send its response and then I overwrite the current document with it.

    Question1 - What I'm doing seems wrong to me. I'm sure I should just be going to another page, a "checkout" page, rather than overwriting the current document. But as my checkout button is not in a form I don't know how to do that.

    Question2 - my second question is that when I submit the form with the delivery details on it, I do not have the parameters I expect when the request hits my servlet. Instead of "CustomerName", "AddressLine1" etc, the only parameters I have are "x" and "y", both of which contain a number (a different number each time).

    Many Thanks,
    Paul

  2. #2
    Join Date
    Jan 2010
    Posts
    7
    To solve my first question I just had to wrap the shopping basket image in an anchor. So instead of:

    <img src="/ptest/page_images/cart.jpg" onclick="checkout();" alt="Go To Checkout" width=83 height=83/>

    I have this:

    <a href="checkout"><img src="/ptest/page_images/cart.jpg" alt="Go To Checkout" width=83 height=83></a>

    Still got the strange x, y parameter thing going on though.

  3. #3
    Join Date
    Jan 2010
    Posts
    7
    I do get all my parameters sent now. For each input in the form I was specifying an id attribute, but not a name attribute. Although specifying a name attribute means that I get all the parameters sent, I still get the x and y parameters sent too. Not really bothered about them now that I am getting my parameters, but still a bit curious.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles