Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Utilities (https://www.askmehelpdesk.com/forumdisplay.php?f=78)
-   -   Dynamic jsp (https://www.askmehelpdesk.com/showthread.php?t=21389)

  • Feb 22, 2006, 09:45 AM
    sarah11282
    Dynamic jsp
    Hi:)

    I am doing a project where I have set up a website. I have added info into mysql.
    How do I dynamically get info.

    I have set up a clothes website, and if the customer uses drop down menus and selects "skirts" and "black" for example, how do I dynamically add the "skirts" and "black" into my sql query, rather than having to manually type them in (using jsp) which would not work anyway as obviously I don't know what the customer will want to view.

    Hope you understand what I'm asking, I'm not extactly great at computer:)

    Any help or links would be greatly appreciated,

    Thanks:)
  • Feb 23, 2006, 12:26 PM
    LTheobald
    Hi Sarah,

    I'll answer your question with more detail tomorrow (when I have more time) but here's the jist of it.

    Your dropdown fields will need a onChange event (e.g. <select onChange="dothis">). This onchange should then submit the form. That will put what the user selected in the request context. Then all you need to do is use request.getParameter("fieldname") to get the value of whatever was in field name. Then you just use that value to build your SQL.

    I know that probably doesn't help. Check the site again tomorrow and I would have posted back a response or someone will beat me to it :) Just can't do it this evening as I'm busy.
  • Feb 24, 2006, 02:25 AM
    LTheobald
    OK, so here's the easiest way to do this...

    You've got a webpage with a form like below:
    Code:

    <form method="post" action="test.jsp" name="productForm">
        <select name="skirts">
            <option value="skirtA">SkirtA</option>
            <option value="skirtB">SkirtB</option>
            <option value="skirtC">SkirtC</option>
        </select>
        <select name="colour" onchange="document.productForm.submit()">
            <option value="black">Black</option>
            <option value="white">White</option>
            <option value="brown">Brown</option>
        </select>
    <form>

    The onChange on the colour drop down should submit the form. You need to make sure that the "action" in the form tag points you the current JSP. So if the file with this code in is called test.jsp, the action should point to test.jsp.

    Then at the top of this page somewhere you want some code similar to the following:
    Code:

    <%
        // Retrieve the selected skirt & colour
        String skirtType = request.getParameter("skirt");
        String colour = request.getParameter("colour");

        // Make sure these aren't null
        if ((skirtType != null) && (colour != null)) {
            // Form the SQL statement
            String sql = "SELECT * FROM SKIRTS WHERE ";
            sql += "SKIRT = "+ skirtType +" AND ";
            sql += "COLOUR = "+ colour;

            // Now you have a SQL statment containing the details of the
            // selected skirt.  Now use the sql variable to query the database.
            // I'm afraid you'll have to figure out that bit yourself though ;)
        }
    %>

    If you have any questions about the above code - post a reply here. I'll answer as soon as possible.

  • All times are GMT -7. The time now is 02:05 AM.