Forum Moderators: open

Message Too Old, No Replies

passing a search value to an onclick

         

mr_l_k

4:11 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



im know its possible but im having a "cant see the wood for the trees" moment

i have various forms that pass a value to a function that in return queries a db ie:

<input type="button" value="Blah" onclick='getXYZ("stuff.php?name=blah")'; action="get" class=button>

what i'd like to do is build a search form button that passes the result and adds it to the end of name=

anyone?

thanks :)

Fotiman

4:43 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should separate your scripting from your markup (avoid using inline onclick attributes). Here's a quick example I threw together. It's quick and dirty, but hopefully will give you some ideas. Note, I prefer not to define event handlers by doing "window.onload =" or "btn.onclick =", but I did for this example to keep it short. I prefer to use the Yahoo UI Library's Event Utiltiy to attach listeners.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
<script type="text/javascript">
// Create a global namespace object to avoid conflicts with other scripts
var WW = window.WW ¦¦ {};
WW.ButtonAppender = function () {
return {
/**
* @param {String} el The id of the button
*/
init : function (el) {
var btn = document.getElementById(el);
if (!btn) { return; }
// Attach an onclick event listener
btn.onclick = function () {
// Display the current value
alert(WW.ButtonAppender.paramlist);
// Then update it to be appended with the new data
WW.ButtonAppender.paramlist += ":" + (new Date()).getSeconds();
};
},
/**
* The String to append
*/
paramlist : 'item1'
};
}(); // Create the Singleton instance of ButtonAppender
window.onload = function () {
WW.ButtonAppender.init('foo');
};
</script>
</head>
<body>
<input type="button" value="Blah" id="foo">
</body>
</html>

mr_l_k

11:29 am on Jun 14, 2007 (gmt 0)

10+ Year Member



the problem is that i have to pass the variable to getXYZ("stuff.php?name=nameofvariable") - the initial js passes the variable to php which then returns the result from mysql db and creates an xml file from the results - that xml file is then processed - to create a google map

Fotiman

2:21 pm on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's fine... in the code where I'm alerting the string, just pass it as a variable to your method instead.