CrazyEngineers
  • jsp doubts....

    lovejeet

    lovejeet

    @lovejeet-etHdkD
    Updated: Oct 21, 2024
    Views: 973
    i have a menu with multiple entries, where only one entry can be selected at a time. i want the page to be redirected to different pages as per the selection of the entry. that is, different pages should be redirected with different entry selections. how to do dat??
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberApr 21, 2012

    I suppose you are using combox to populate date and then selecting it.
    Initially on page load do not select anything then add following like this
    <select id="Find" size="1" onchange="javascript:yourFunction()" style="position:relative;" >
    <option value="Anchor1">Anchor1</option>
    <option value="Anchor2">Anchor2</option>
    <option value="Anchor3">Anchor3</option>
    <option value="Anchor4">Anchor4</option>
    </select><br> 
    you can send pass parameter also.
    then in yourFunction() javascript: add <a href="https://bytes.com/topic/javascript/answers/656764-how-redirect-page-using-javascript" target="_blank" rel="nofollow noopener noreferrer">how to redirect a page using javascript? - Javascript</a>
    You can use <a href="https://www.w3schools.com/jsref/prop_loc_href.asp" target="_blank" rel="nofollow noopener noreferrer">Location href Property</a>
    Are you sure? This action cannot be undone.
    Cancel
  • lovejeet

    MemberApr 21, 2012

    hey thanks for the reply, but you didn't got the question i guess. sorry if was a bit ambiguous.
    it's for combo box only, where there are several entities, say x, y and z. what i want is that when user selects x, it should redirect to another page say abc.jsp, for y, it should redirect to pqr.jsp and so on....
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberApr 21, 2012

    what are xyz....
    if these are radio buttons then just use OnClick() event and call the javascript function.
    Are you sure? This action cannot be undone.
    Cancel
  • lovejeet

    MemberApr 21, 2012

    xyz are different entities like anchor1, anchor2 in your example. n ya, onclick() method is a nice solution to that....
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 23, 2012

    Wait... Lemme say... You got a combobox with values Page 1, Page 2, Page 3. And upon selecting them, they should redirect you to One.jsp, Two.jsp, and Three.jsp accordingly. If this is the case, you can try this:
    <select name="page" id="page" onchange="redirectPage(this);">
      <option value="One.jsp">Page 1</option>
      <option value="Two.jsp">Page 2</option>
      <option value="Three.jsp">Page 3</option>
    </select>
     
    <script type="text/javascript">
    function redirectPage(which)
    {
      location.href = which.value;
    }
    </script>
    This works in almost all the browsers. Checked just now. Try and let me know... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • lovejeet

    MemberApr 24, 2012

    hey thanks a lot #-Link-Snipped-#. yup done. now the next problem is the drop down menu's. i want my 3rd dropdown menu to be based on the values of my 2nd menu, and dat to be dependent on the 1st one. how to do that now???
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 24, 2012

    lovejeet
    hey thanks a lot #-Link-Snipped-#. yup done. now the next problem is the drop down menu's. i want my 3rd dropdown menu to be based on the values of my 2nd menu, and dat to be dependent on the 1st one. how to do that now???
    So it means 3 individual dropdowns huh???
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 24, 2012

    You mean like this? The value of the next combo box is dependent on the previous one???

    [​IMG]
    Are you sure? This action cannot be undone.
    Cancel
  • lovejeet

    MemberApr 24, 2012

    yup. like this. it includes dynamic selection for some dropdown menu's using database too. 😕
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 25, 2012

    lovejeet
    yup. like this. it includes dynamic selection for some dropdown menu's using database too. 😕
    I guess its getting complicated... Then you got two choices:
    1. Ajax Update
    2. Page Refresh

    Which one do you prefer?
    Are you sure? This action cannot be undone.
    Cancel
  • lovejeet

    MemberApr 25, 2012

    i do not prefer ajax. wanna keep it as the last option. i tried it for two drop down menu's and it wasn't much tought. but when it comes to more than two and when retrieving the values from database, its all over my head....😛
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 25, 2012

    If you were using ASP.net, it is damn easy... Check this out: #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 25, 2012

    And I saw this in StackOverflow. Seems promising. Read on...

    You need to download JQuery (JavaScript framework) and include that in the head section of the HTML.
    <script type="text/javascript" src="jquery.js"></script>
    Inside the OnChange function for first dropdown call the function to populate the subsequent dropdowns.
    $(document).ready(function() {
        $(
    '#DDIdParent').change(function() {
            
    populateChildDropdown('DDIdParent');
        });
    });
    The function is a s follows:
    function populateChildDropdown(ddId) {
        var 
    dd = $('#' ddId);
        $.
    getJSON('json/mapping?dd=' ddId, function(opts) {
            $(
    '>option'dd).remove(); // Clear old options first.
            
    if (opts) {
                $.
    each(opts, function(keyvalue) {
                    
    dd.append($('<option/>').val(key).text(value));
                });
            } else {
                
    dd.append($('<option/>').text("Please select the parent DD"));
            }
        });
    }
    Inside your Servlet's doGet method, you will have a code like this to get the value of subsequent dropdown from the database as a JSON String:
            Map options yourDao.callDatabase(parameter);
            
    String json = new Gson().toJson(options);
            
    res.setContentType("application/json");
            
    res.setCharacterEncoding("UTF-8");
            
    res.getWriter().write(json);
    You need to map the json/mapping to your servlet inside web.xml. You can also pass parameters from the jquery function if needed.
    Are you sure? This action cannot be undone.
    Cancel
  • lovejeet

    MemberApr 25, 2012

    sir ji i want that in jsp, no php or .net allowed...😔
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberApr 25, 2012

    Without ajax you need to refresh the page.
    using onChange() even submit the page with some flag. and on sevlet/ jsp, you are using, get all the values. now fetch values for next drop down menu on basis of latest dropdown menu. and come back to the page with all values. and populate the your latest dropdown menu and set all the previous values.
    why not Ajax. you are just fetching data from database.😀
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 26, 2012

    lovejeet
    sir ji i want that in jsp, no php or .net allowed...😔
    Sir Ji! It is JSP! 😀

    And if you really know JavaScript, those things inside PHP block are JavaScripts. No where I have used PHP! Lolz...
    Are you sure? This action cannot be undone.
    Cancel
  • Prasad Ajinkya

    MemberApr 27, 2012

    This smells like an assignment 😀

    But perhaps you need to do this via simply Javascript.
    Are you sure? This action cannot be undone.
    Cancel
  • lovejeet

    MemberApr 27, 2012

    @praveen- oops..... sorry that was a bit confusing by that header of 'php'. will try the code.
    @kidakaka- hahaha.......yup, a part of my project only, but what all i could try was for two menu's and that to without the database. rest i need to take the help...😀
    Are you sure? This action cannot be undone.
    Cancel
  • Prasad Ajinkya

    MemberApr 27, 2012

    Here's what you do -
    Declare a javascript array for the URLs.

    In the dropdown, the values of each option should map to the index of this URL array.

    When the form is submitted, simply invoke a function which will take the value of the dropdown, retrieve the particular URL from that value and change the location to that.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberApr 27, 2012

    kidakaka
    Here's what you do -
    Declare a javascript array for the URLs.

    In the dropdown, the values of each option should map to the index of this URL array.

    When the form is submitted, simply invoke a function which will take the value of the dropdown, retrieve the particular URL from that value and change the location to that.
    Even then it becomes an AJAX Call right?
    Are you sure? This action cannot be undone.
    Cancel
  • Prasad Ajinkya

    MemberApr 27, 2012

    Nope. Its still Javascript. Why go into the trouble of AJAX?
    <html>
    <script language="Javascript">
    function doMyThang() { var urlArray = ["https://google.com","https://crazyengineers.com","https://kidakaka.com"];window.location = urlArray[document.quickUrl.urlDropdown.value-1];
    }
    </script>
    <body>
    <form name="quickUrl">
    <select name="urlDropdown" >
        <option value="1">Google</option>
        <option value="2">CE</option>
        <option value="3">Kidakaka</option>
    </select>
    <input type="button" value="do my thang!" onClick="javascript:doMyThang();" />
    </form>
    </body>
    </html>
    Here's the code of what I was thinking.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register