views:

29

answers:

3

Greetings, all! I am fairly new to JS (my background is in C++, enterprise languages like assembly and COBOL, and some light .NET), so I was wondering if I could get some advice regarding how to send variable information from one of two text boxes to some javascript and then have the JS do some basic checks and such. Here's the pseudocode for what I am trying to do:

<form = webForm>
<p>
_____________
textboxPeople| searchIcon      //a text box to search an online phonebook at my company.
-------------                  //It has a little "magnifying glass" icon to search
                               //(onClick), but I would also like them to be able to 
                               //search by hitting the "enter" key on their keyboards.
</p>
<p>

_____________
texboxIntranet| searchIcon     //Same as above search textbox, but this one is used if
-------------                  //the user wishes to search my corporate intranet site.

</form>

So ends the front-facing basic form that I would like to use. Now, onClick or onEnter, I would like the form to pass the contents of the text box used as well as an identifier such as "People" or "Intranet," depending on which box is used, to some basic JS on the back end:

begin JavaScript:

if(identifier = 'People')
  fire peopleSearch();

else
if(identifier = 'Intranet')
  fire intranetSearch();


function peopleSearch()
{
    http://phonebook.corporate.com/query=submittedValue    //This will take the person
                                             //that the user submitted in the form and
                                             //place it at the end of a URL, after which
                                             //it will open said complete URL in the 
                                             //same window.
}

function intranetSearch()
{
    http://intranet.corporate.com/query=submittedValue    //This will function in the
                                             //same way as the people search function.
}

end JavaScript

Any thoughts/suggestions would be greatly appreciated. Thank you all in advance!

A: 

HTML forms by default are submitted by hitting Enter key - so you do not have to use any JS. All you have to do is to create two plain HTML forms:

<form action="http://phonebook.corporate.com/" method="Get">
    <input type="text" name="query" />
</form>

<form action="http://intranet.corporate.com/" method="Get">
    <input type="text" name="query" />
</form>
el.pescado
A: 
<form id="search-form">
    <div><input type="text" name="query" id="query-people" /> <input type="submit" value="Search phonebook" /></div>
    <div><input type="text" name="query" id="query-intranet" /> <input type="submit" value="Search intranet" /></div>
</form>

<script>
    var search_form = document.getElementById('search-form'),
        query_people = document.getElementById('query-people'),
        query_intranet = document.getElementById('query-intranet');
    search_form.onsubmit = function() {
        if (query_people.value) {
            people_search();
        }
        else if (query_intranet.value) {
            intranet_search();
        }
        return false;
    };

    function people_search() {
        window.location = 'http://phonebook.corporate.com/?query='+ encodeURIComponent(query_people.value);
    }

    function intranet_search() {
        window.location = 'http://intranet.corporate.com/?query='+ encodeURIComponent(query_intranet.value);
    }
</script>

Of course, there are other - more elegant - ways to do it...

Oblio
A: 

First of all... welcome to the Web development world (it's way more sexy than Cobol... LOL). Since you're fairly new to JavaScript, I would like to suggest that you start with jQuery. It's way easier and cleaner than doing the same task in traditional JS. Here's the code that for the two search boxes:

HTML:

<form id="peopleform" action="peoplesearch.aspx" method="post">
  <label for="peoplesearch">People:</label>
  <input type="text" name="peoplesearch" id="peoplesearch">
  <input type="image" id="peoplebutton" src="magnifyingglass.gif" alt="Search for people.">
</form>

<form id="intranetform" action="intranetsearch.aspx" method="post">
  <label for="intranetsearch">Intranet:</label>
  <input type="text" name="intranetsearch" id="intranetsearch">
  <input type="image" id="intranetbutton" src="magnifyingglass.gif" alt="Search the Intranet.">
</form>

JavaScript:

<script type="text/javascript">
  $(document).ready(function(){ 
    /* People Search */
    $('#peoplesearch').keydown(function(e){ /* Detect key presses in the people search field */
      if(e.keyCode==13) { /* Detect enter */
        $('#peopleform').submit(); /* Submit people search form */
      }
    });

    $('#peoplebutton').click(function(){ /* Detect click on people search magnifying glass */
      $('#peopleform').submit(); /* Submit people search form */
    });

    /* Intranet Search */
    $('#intranetsearch').keydown(function(e){ /* Detect key presses in the Intranet search field */
      if(e.keyCode==13) { /* Detect enter */
        $('#intranetform').submit(); /* Submit Intranet search form */
      }
    });

    $('#intranetbutton').click(function(){ /* Detect click on Intranet search magnifying glass */
      $('#intranetform').submit(); /* Submit Intranet search form */
    });
  });
</script>

I split the search boxes into two forms. That way you can avoid passing an identifier and the code becomes more obvious (you submit to two different pages on the server). You need to hook up jQuery, add your magnifying glass images and write the server side stuff, but I hope this gets you started.

Gert G
So, should these two code segments be contained in the same file, or should they be in different files and then should I put some sort of reference to the backend file in the front end file?
Enyalius
You put the script portion in the the head of the HTML document and the HTML portion in the body of the HTML document. You could separate the script portion by putting it in a separate file and load it in a script clause in the head (e.g. <script type="text/javascript" src="myjavascript.js"></script>), but it's not necessary. Then you would need to have two files (in the example above; peoplesearch.aspx and intranetsearch.aspx) that takes care of the search. These file will contain server side code that interacts with your backend systems and then build the results page.
Gert G
And don't forget to download jQuery and put a reference to it in the head of the page (e.g. <script type="text/javascript" src="jquery.min.js"></script>. The code above won't work without the jQuery library.
Gert G