Forum Moderators: open

Message Too Old, No Replies

Get text input into javascript url

Get text input into select Onchange url

         

PSWorx

5:48 pm on May 20, 2006 (gmt 0)

10+ Year Member



Ok the subject and meta description do no justice for the true topic as its a bit of pain to word, here goes:

I have a form, with two select menus and also an input area,

Both drop downs on change send the new url to the browser for a page reload onChange, these work fine on both drop downs.

I wanted to get the text from the input box (onchange) into the url of both drop down menus.

Code for the drop down onChange is as follows (using php here aswell):

onChange="if(options[selectedIndex].value) window.location.href=\'index.php?apid='.$_GET['apid'].'&psid='.$_GET['psid'].'&pg='.$_GET['pg'].'&colsort='.$_GET['colsort'].'&sortby='.$_GET['sortby'].'&gf=\'+(options[selectedIndex].value)+\'&ss='.$_GET['ss'].'\'"

I need to get the user input in the text input area into the var value ss at the end of the js url.

Im under the impression this will be a small function to inject the value of the text area onChange into the defined area of the js url for both select menus.

Any help here would be great

TIA

jshanman

2:41 pm on May 22, 2006 (gmt 0)

10+ Year Member



So you have:

<select id="s1" name="s1" onchange="go(this)">
<option value="s1value1">s1 value1</option>
<option value="s1value2">s1 value2</option>
</select>
<input type="text" name="t" id="t">

And in JS:
function go(s) {
var tvalue = document.getElementById("t").value;
if (tvalue == "") tvalue = '<?php echo $_GET['ss']?>';
window.location.href = "page.php?gf="+s.options[this.selectedIndex].value+"&ss="+tvalue;
}

I would suggest storing $_GET['ss'] in another variable, and making sure there are no quotes in it before trying to insert it into javascript.

Does this help?
- JS

PSWorx

3:51 pm on May 22, 2006 (gmt 0)

10+ Year Member



Sorry ill run through the code might be clearer with regards to my intention:

I have two select menus similar to:

<select name="select1" onChange="if(options[selectedIndex].value) window.location.href='index.php?select1='+(options[selectedIndex].value)+'&searchString='+GET VALUE FROM JAVASCRIPT+''">
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
<option value="4"></option>
</select>

<select name="select2" onChange="if(options[selectedIndex].value) window.location.href='index.php?select2='+(options[selectedIndex].value)+'&searchString='+GET VALUE FROM JAVASCRIPT+''">
<option value="a"></option>
<option value="b"></option>
<option value="c"></option>
<option value="d"></option>
</select>

And also a text input:

<input type="text" name="searchString" value="" size="20" />

What i would like to do here is, when the value of the text input box is changed (without submitting the form), have its value inserted into the end of the onChange URLS (&searchString='+GET VALUE FROM JAVASCRIPT+') in the two select menus so when the menus are used after text has been inserted into the box, the menus will use that text input as part of the filters being passed to the script.

I am assuming that i would need a small function to determine if the content has changed in the text input box and for it to then insert its value into the searchString parameter of the url in the select menu's onChange event handler (if thats what it should be refered to as lol)

TIA

jshanman

4:50 pm on May 22, 2006 (gmt 0)

10+ Year Member



You don't really need to change the value of the onchange script. Using the code I posted still solves your problem.

If a user types something into the textbox, nothing will happen.

But if the user types something into the text box, then selects either of the select boxes, it will run the "go" function, which sends the current select box.

It checks the text box for text and defaults to the current php value if the current text box is empty (you could also just set the text box value to the php variable)

It then sets the url to contain both the selectedIndex value and the textbox text.

- JS

PSWorx

5:10 pm on May 22, 2006 (gmt 0)

10+ Year Member



Cheers ill take a look at that properly i was a bit concerned it was reading that value of the text box after the form submission but i guess i was being a bit hasty,

Thanks for your time