Javascript/Function to choose between target pages?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Penny

    #1

    Javascript/Function to choose between target pages?

    Hi all,

    I've built a simple search <Form> on a web page that is intended to allow
    the user to search a record store database. There is a drop down box where
    the user can choose either 'Artist' or 'Title', next is a text box where
    they enter their 'Keyword' and next is the search button. For example the
    user may choose 'Artist', type 'Bob Dylan', click the search button and be
    presented with a page showing all the details of the Bob Dylan recordings
    the record store have in stock.

    Thing is, the 'Results' details for Artists and Recordings have different
    fields so I use two different pages to display them. Therefore I need a way
    for the page to check whether 'Artist' or 'Recording' is selected in the
    drop down box and then open the appropriate page. My database search and
    connection are fine I just need the javascript and call from the form.

    Any ideas?

    Penny


  • Randell D.

    #2
    Re: Javascript/Function to choose between target pages?

    Penny wrote:[color=blue]
    > Hi all,
    >
    > I've built a simple search <Form> on a web page that is intended to allow
    > the user to search a record store database. There is a drop down box where
    > the user can choose either 'Artist' or 'Title', next is a text box where
    > they enter their 'Keyword' and next is the search button. For example the
    > user may choose 'Artist', type 'Bob Dylan', click the search button and be
    > presented with a page showing all the details of the Bob Dylan recordings
    > the record store have in stock.
    >
    > Thing is, the 'Results' details for Artists and Recordings have different
    > fields so I use two different pages to display them. Therefore I need a way
    > for the page to check whether 'Artist' or 'Recording' is selected in the
    > drop down box and then open the appropriate page. My database search and
    > connection are fine I just need the javascript and call from the form.
    >
    > Any ideas?
    >
    > Penny
    >
    >[/color]

    I'm a newbie with javascript (about six months or so) so I hope I can
    help point you in the right direction...

    If you have a form called formName, and a select box called mySelectBox
    then selectedItem will have the value of the selected OPTION tag.

    var selectedItem = document.formNa me.mySelectBox. selectedIndex;

    Thus, something like

    <form name="formName" method="POST">
    <select name="mySelectB ox">
    <option value="artist"> Artist</option>
    <option value="recordin g">Recording </option>
    </select>

    Depending on what you had selected, selectedItem would have the value
    'artist' or 'recording'.

    I believe I'm correct above... Does this give you some direction?

    randelld
    </form>

    Comment

    • Penny

      #3
      Re: Javascript/Function to choose between target pages?

      Randell D,

      Thanks for your help. Yes, I think you are giving me some direction.

      I guess now the 'Submit' buttons onClick event must call a function that
      tests the value of selectedIndex and then redirects the browser to the
      appropriate page.

      Do you know the code I can use to direct the browser to a given page from
      within the called function?

      Thanks again.

      Penny.


      "Randell D." <reply.via.news .group.only.tha nks@fiprojects. moc> wrote in
      message news:jlFQd.4061 86$8l.384446@pd 7tw1no...[color=blue]
      > Penny wrote:[color=green]
      > > Hi all,
      > >
      > > I've built a simple search <Form> on a web page that is intended to[/color][/color]
      allow[color=blue][color=green]
      > > the user to search a record store database. There is a drop down box[/color][/color]
      where[color=blue][color=green]
      > > the user can choose either 'Artist' or 'Title', next is a text box where
      > > they enter their 'Keyword' and next is the search button. For example[/color][/color]
      the[color=blue][color=green]
      > > user may choose 'Artist', type 'Bob Dylan', click the search button and[/color][/color]
      be[color=blue][color=green]
      > > presented with a page showing all the details of the Bob Dylan[/color][/color]
      recordings[color=blue][color=green]
      > > the record store have in stock.
      > >
      > > Thing is, the 'Results' details for Artists and Recordings have[/color][/color]
      different[color=blue][color=green]
      > > fields so I use two different pages to display them. Therefore I need a[/color][/color]
      way[color=blue][color=green]
      > > for the page to check whether 'Artist' or 'Recording' is selected in the
      > > drop down box and then open the appropriate page. My database search and
      > > connection are fine I just need the javascript and call from the form.
      > >
      > > Any ideas?
      > >
      > > Penny
      > >
      > >[/color]
      >
      > I'm a newbie with javascript (about six months or so) so I hope I can
      > help point you in the right direction...
      >
      > If you have a form called formName, and a select box called mySelectBox
      > then selectedItem will have the value of the selected OPTION tag.
      >
      > var selectedItem = document.formNa me.mySelectBox. selectedIndex;
      >
      > Thus, something like
      >
      > <form name="formName" method="POST">
      > <select name="mySelectB ox">
      > <option value="artist"> Artist</option>
      > <option value="recordin g">Recording </option>
      > </select>
      >
      > Depending on what you had selected, selectedItem would have the value
      > 'artist' or 'recording'.
      >
      > I believe I'm correct above... Does this give you some direction?
      >
      > randelld
      > </form>[/color]


      Comment

      • RobB

        #4
        Re: Javascript/Function to choose between target pages?

        Randell D. wrote:[color=blue]
        > Penny wrote:[color=green]
        > > Hi all,
        > >
        > > I've built a simple search <Form> on a web page that is intended to[/color][/color]
        allow[color=blue][color=green]
        > > the user to search a record store database. There is a drop down[/color][/color]
        box where[color=blue][color=green]
        > > the user can choose either 'Artist' or 'Title', next is a text box[/color][/color]
        where[color=blue][color=green]
        > > they enter their 'Keyword' and next is the search button. For[/color][/color]
        example the[color=blue][color=green]
        > > user may choose 'Artist', type 'Bob Dylan', click the search button[/color][/color]
        and be[color=blue][color=green]
        > > presented with a page showing all the details of the Bob Dylan[/color][/color]
        recordings[color=blue][color=green]
        > > the record store have in stock.
        > >
        > > Thing is, the 'Results' details for Artists and Recordings have[/color][/color]
        different[color=blue][color=green]
        > > fields so I use two different pages to display them. Therefore I[/color][/color]
        need a way[color=blue][color=green]
        > > for the page to check whether 'Artist' or 'Recording' is selected[/color][/color]
        in the[color=blue][color=green]
        > > drop down box and then open the appropriate page. My database[/color][/color]
        search and[color=blue][color=green]
        > > connection are fine I just need the javascript and call from the[/color][/color]
        form.[color=blue][color=green]
        > >
        > > Any ideas?
        > >
        > > Penny
        > >
        > >[/color]
        >
        > I'm a newbie with javascript (about six months or so) so I hope I can[/color]
        [color=blue]
        > help point you in the right direction...[/color]

        No crime in being a newb - but you should *test* your assumptions
        before posting them, so as not to mislead. Even experienced posters do.
        [color=blue]
        > If you have a form called formName, and a select box called[/color]
        mySelectBox[color=blue]
        > then selectedItem will have the value of the selected OPTION tag.
        >
        > var selectedItem = document.formNa me.mySelectBox. selectedIndex;[/color]

        No - it will have the index (position) of the selected option in the
        options[] collection.
        [color=blue]
        > Thus, something like
        >
        > <form name="formName" method="POST">
        > <select name="mySelectB ox">
        > <option value="artist"> Artist</option>
        > <option value="recordin g">Recording </option>
        > </select>
        >
        > Depending on what you had selected, selectedItem would have the value[/color]
        [color=blue]
        > 'artist' or 'recording'.[/color]

        (snip)

        Nope...it would hold the integer 0 or 1. You get the value by
        specifying it:

        document.formNa me.mySelectBox. value

        ....although to support older browsers, this:

        var s = document.formNa me.mySelectBox;
        var selectedItem = s.options[s.selectedIndex].value

        ....is more common.

        Penny...why not use radios? If there are only two (or maybe three)
        options it's a logical choice. More to the point: why do you need to do
        anything at the client (browser) at all? The server should be able to
        process the form data and respond accordingly...

        Comment

        • Penny

          #5
          Re: Javascript/Function to choose between target pages?

          Thanks Rob,

          The user must choose whether they are searching on Artists or Titles and
          each of those two has a different results page so this page must itself
          decide which page to redirect to. Also, that's the way it usually seems to
          be done on record store websites (rollingstone.c om for example, but I can't
          see figure how they've done it in their source code).

          Regards,
          Penny.

          "RobB" <ferndoc9@hotma il.com> wrote in message
          news:1108572916 .072919.102260@ f14g2000cwb.goo glegroups.com.. .[color=blue]
          > Randell D. wrote:[color=green]
          > > Penny wrote:[color=darkred]
          > > > Hi all,
          > > >
          > > > I've built a simple search <Form> on a web page that is intended to[/color][/color]
          > allow[color=green][color=darkred]
          > > > the user to search a record store database. There is a drop down[/color][/color]
          > box where[color=green][color=darkred]
          > > > the user can choose either 'Artist' or 'Title', next is a text box[/color][/color]
          > where[color=green][color=darkred]
          > > > they enter their 'Keyword' and next is the search button. For[/color][/color]
          > example the[color=green][color=darkred]
          > > > user may choose 'Artist', type 'Bob Dylan', click the search button[/color][/color]
          > and be[color=green][color=darkred]
          > > > presented with a page showing all the details of the Bob Dylan[/color][/color]
          > recordings[color=green][color=darkred]
          > > > the record store have in stock.
          > > >
          > > > Thing is, the 'Results' details for Artists and Recordings have[/color][/color]
          > different[color=green][color=darkred]
          > > > fields so I use two different pages to display them. Therefore I[/color][/color]
          > need a way[color=green][color=darkred]
          > > > for the page to check whether 'Artist' or 'Recording' is selected[/color][/color]
          > in the[color=green][color=darkred]
          > > > drop down box and then open the appropriate page. My database[/color][/color]
          > search and[color=green][color=darkred]
          > > > connection are fine I just need the javascript and call from the[/color][/color]
          > form.[color=green][color=darkred]
          > > >
          > > > Any ideas?
          > > >
          > > > Penny
          > > >
          > > >[/color]
          > >
          > > I'm a newbie with javascript (about six months or so) so I hope I can[/color]
          >[color=green]
          > > help point you in the right direction...[/color]
          >
          > No crime in being a newb - but you should *test* your assumptions
          > before posting them, so as not to mislead. Even experienced posters do.
          >[color=green]
          > > If you have a form called formName, and a select box called[/color]
          > mySelectBox[color=green]
          > > then selectedItem will have the value of the selected OPTION tag.
          > >
          > > var selectedItem = document.formNa me.mySelectBox. selectedIndex;[/color]
          >
          > No - it will have the index (position) of the selected option in the
          > options[] collection.
          >[color=green]
          > > Thus, something like
          > >
          > > <form name="formName" method="POST">
          > > <select name="mySelectB ox">
          > > <option value="artist"> Artist</option>
          > > <option value="recordin g">Recording </option>
          > > </select>
          > >
          > > Depending on what you had selected, selectedItem would have the value[/color]
          >[color=green]
          > > 'artist' or 'recording'.[/color]
          >
          > (snip)
          >
          > Nope...it would hold the integer 0 or 1. You get the value by
          > specifying it:
          >
          > document.formNa me.mySelectBox. value
          >
          > ...although to support older browsers, this:
          >
          > var s = document.formNa me.mySelectBox;
          > var selectedItem = s.options[s.selectedIndex].value
          >
          > ...is more common.
          >
          > Penny...why not use radios? If there are only two (or maybe three)
          > options it's a logical choice. More to the point: why do you need to do
          > anything at the client (browser) at all? The server should be able to
          > process the form data and respond accordingly...
          >[/color]


          Comment

          • RobB

            #6
            Re: Javascript/Function to choose between target pages?

            Penny wrote:[color=blue]
            > Thanks Rob,
            >
            > The user must choose whether they are searching on Artists or Titles[/color]
            and[color=blue]
            > each of those two has a different results page so this page must[/color]
            itself[color=blue]
            > decide which page to redirect to. Also, that's the way it usually[/color]
            seems to[color=blue]
            > be done on record store websites (rollingstone.c om for example, but I[/color]
            can't[color=blue]
            > see figure how they've done it in their source code).
            >
            > Regards,
            > Penny.
            >
            > "RobB" <ferndoc9@hotma il.com> wrote in message
            > news:1108572916 .072919.102260@ f14g2000cwb.goo glegroups.com.. .[color=green]
            > > Randell D. wrote:[color=darkred]
            > > > Penny wrote:
            > > > > Hi all,
            > > > >
            > > > > I've built a simple search <Form> on a web page that is[/color][/color][/color]
            intended to[color=blue][color=green]
            > > allow[color=darkred]
            > > > > the user to search a record store database. There is a drop[/color][/color][/color]
            down[color=blue][color=green]
            > > box where[color=darkred]
            > > > > the user can choose either 'Artist' or 'Title', next is a text[/color][/color][/color]
            box[color=blue][color=green]
            > > where[color=darkred]
            > > > > they enter their 'Keyword' and next is the search button. For[/color]
            > > example the[color=darkred]
            > > > > user may choose 'Artist', type 'Bob Dylan', click the search[/color][/color][/color]
            button[color=blue][color=green]
            > > and be[color=darkred]
            > > > > presented with a page showing all the details of the Bob Dylan[/color]
            > > recordings[color=darkred]
            > > > > the record store have in stock.
            > > > >
            > > > > Thing is, the 'Results' details for Artists and Recordings have[/color]
            > > different[color=darkred]
            > > > > fields so I use two different pages to display them. Therefore[/color][/color][/color]
            I[color=blue][color=green]
            > > need a way[color=darkred]
            > > > > for the page to check whether 'Artist' or 'Recording' is[/color][/color][/color]
            selected[color=blue][color=green]
            > > in the[color=darkred]
            > > > > drop down box and then open the appropriate page. My database[/color]
            > > search and[color=darkred]
            > > > > connection are fine I just need the javascript and call from[/color][/color][/color]
            the[color=blue][color=green]
            > > form.[color=darkred]
            > > > >
            > > > > Any ideas?
            > > > >
            > > > > Penny[/color][/color][/color]

            (snip)

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
            <html>
            <head>
            <title>untitled </title>
            <script type="text/javascript">

            function checkform(f)
            {
            var els = f.elements,
            msg = '';
            if (/^\s*$/.test(els.keywo rd.value))
            {
            msg += 'Please enter a keyword.\n';
            msg += '\nThank you, dude.\n';
            alert(msg);
            els.keyword.foc us();
            return false;
            }
            else
            {
            f.action = ['artist.html',' title.html'][els.what[0].checked?0:1];
            return true;
            }
            }

            </script>
            </head>
            <body>
            <form action="artist. html" onsubmit="retur n checkform(this) ">
            <h4>Search for:</h4>
            <ul>
            <li>artist
            <input type="radio"
            name="what"
            value="artist"
            checked="checke d" />
            </li>
            <li>
            title &nbsp;
            <input type="radio"
            name="what"
            value="recordin gs" />
            </li>
            </ul>
            <h4>Keyword <input type="text" name="keyword" value="" /></h4>
            <input type="submit" value="search" />
            </form>
            </body>
            </html>

            No sure if that helps. Server-based processing is always better; you
            can dynamically assemble the entire response page based on query data.
            JS-disabled, no problem, HTML fine. Just fyi. ;-)

            Comment

            • Penny

              #7
              Re: Javascript/Function to choose between target pages?

              Thanks for the help Rob,

              I'm having a good look at your code and I'll see what I can do with it.

              Penny



              "RobB" <ferndoc9@hotma il.com> wrote in message
              news:1108618980 .375457.122960@ c13g2000cwb.goo glegroups.com.. .[color=blue]
              > Penny wrote:[color=green]
              > > Thanks Rob,
              > >
              > > The user must choose whether they are searching on Artists or Titles[/color]
              > and[color=green]
              > > each of those two has a different results page so this page must[/color]
              > itself[color=green]
              > > decide which page to redirect to. Also, that's the way it usually[/color]
              > seems to[color=green]
              > > be done on record store websites (rollingstone.c om for example, but I[/color]
              > can't[color=green]
              > > see figure how they've done it in their source code).
              > >
              > > Regards,
              > > Penny.
              > >
              > > "RobB" <ferndoc9@hotma il.com> wrote in message
              > > news:1108572916 .072919.102260@ f14g2000cwb.goo glegroups.com.. .[color=darkred]
              > > > Randell D. wrote:
              > > > > Penny wrote:
              > > > > > Hi all,
              > > > > >
              > > > > > I've built a simple search <Form> on a web page that is[/color][/color]
              > intended to[color=green][color=darkred]
              > > > allow
              > > > > > the user to search a record store database. There is a drop[/color][/color]
              > down[color=green][color=darkred]
              > > > box where
              > > > > > the user can choose either 'Artist' or 'Title', next is a text[/color][/color]
              > box[color=green][color=darkred]
              > > > where
              > > > > > they enter their 'Keyword' and next is the search button. For
              > > > example the
              > > > > > user may choose 'Artist', type 'Bob Dylan', click the search[/color][/color]
              > button[color=green][color=darkred]
              > > > and be
              > > > > > presented with a page showing all the details of the Bob Dylan
              > > > recordings
              > > > > > the record store have in stock.
              > > > > >
              > > > > > Thing is, the 'Results' details for Artists and Recordings have
              > > > different
              > > > > > fields so I use two different pages to display them. Therefore[/color][/color]
              > I[color=green][color=darkred]
              > > > need a way
              > > > > > for the page to check whether 'Artist' or 'Recording' is[/color][/color]
              > selected[color=green][color=darkred]
              > > > in the
              > > > > > drop down box and then open the appropriate page. My database
              > > > search and
              > > > > > connection are fine I just need the javascript and call from[/color][/color]
              > the[color=green][color=darkred]
              > > > form.
              > > > > >
              > > > > > Any ideas?
              > > > > >
              > > > > > Penny[/color][/color]
              >
              > (snip)
              >
              > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
              > "http://www.w3.org/TR/html4/strict.dtd">
              > <html>
              > <head>
              > <title>untitled </title>
              > <script type="text/javascript">
              >
              > function checkform(f)
              > {
              > var els = f.elements,
              > msg = '';
              > if (/^\s*$/.test(els.keywo rd.value))
              > {
              > msg += 'Please enter a keyword.\n';
              > msg += '\nThank you, dude.\n';
              > alert(msg);
              > els.keyword.foc us();
              > return false;
              > }
              > else
              > {
              > f.action = ['artist.html',' title.html'][els.what[0].checked?0:1];
              > return true;
              > }
              > }
              >
              > </script>
              > </head>
              > <body>
              > <form action="artist. html" onsubmit="retur n checkform(this) ">
              > <h4>Search for:</h4>
              > <ul>
              > <li>artist
              > <input type="radio"
              > name="what"
              > value="artist"
              > checked="checke d" />
              > </li>
              > <li>
              > title &nbsp;
              > <input type="radio"
              > name="what"
              > value="recordin gs" />
              > </li>
              > </ul>
              > <h4>Keyword <input type="text" name="keyword" value="" /></h4>
              > <input type="submit" value="search" />
              > </form>
              > </body>
              > </html>
              >
              > No sure if that helps. Server-based processing is always better; you
              > can dynamically assemble the entire response page based on query data.
              > JS-disabled, no problem, HTML fine. Just fyi. ;-)
              >[/color]


              Comment

              Working...