Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javascript - Function Parameter As Variable

2:23 am on Oct 28, 2011 (gmt 0)

10+ Year Member


I am having a problem with an onclick event, inside a div, that is in another div which the content of said div is an AJAX'd search result. The onclick event basically renews the search using a different parameter, that is found during the first search - basically the function checks to see whether or not a city name is spelled correctly or not. If not, then a button is presented to click to change the search results.

AJAX / Javascript Code:

<script language="javascript">
function ajaxgetter(variable) {
var stuff = variable;
var mygetrequest = new ajaxRequest();
mygetrequest.onreadystatechange = function() {
if (mygetrequest.readyState == 4) {
if (mygetrequest.status == 200 || window.location.href.indexOf("http") == -1) {
document.getElementById("result").innerHTML = mygetrequest.responseText;
alert("An error has occured making the request");

var namevalue4 = encodeURIComponent(document.getElementById("name9"+stuff).value);
var agevalue4 = encodeURIComponent(document.getElementById("age9"+stuff).value);
var utvalue4 = encodeURIComponent(document.getElementById("ut9"+stuff).value);
document.getElementById("result").innerHTML = "<center><b>Loading...</b></center><br><center><img src='images/ajax-loader1.gif' /></center>";
mygetrequest.open("GET", "newdealerfinder.php?location="+namevalue4+"&distance="+agevalue4+"&ut="+utvalue4"&r="+ Math.random(), true);

The code above is using a variable brought up by the 2nd search. Here is the relevant PHP code to do the 2nd search:

if($miles1 > 2) {
echo "<form action='' method='get' />
<input type='hidden' value='$city->lat,$city->lng' id='name9$javacounter' name='name9$javacounter' />
<input type='hidden' value='$currentunixtime' id='ut9$javacounter' name='ut9$javacounter' />
<input type='hidden' value='$distance' id='age9$javacounter' name='age9$javacounter' />
<input type='button' value='Change Location' onClick='ajaxgetter($javacounter)' />

By using Firebug, I can see that the proper ids are being set, and if anyone wants to take a look at the script in question, I can post a link.

8:30 am on Oct 28, 2011 (gmt 0)

10+ Year Member

Disregard, its working now. All I did was write the same code out again.