Jump to content

Javascript Variable


DaVuLf

Recommended Posts

Hi there,

 

I am having trouble with a couple of things on my file. First off, I have passed a variable through the URL, and it is not getting parsed properly. For instance:

 

var name = getValue("testinput");
name.replace(/\+/g," ");

 

That should eliminate the +'s between words. It does not.

 

Second, I am trying to pull a value from a recordset:

 

var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\PCL\\test.mdb;Persist Security Info=False";
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "select * from tablename WHERE term_name='"+userInput+"'";
rs.Open(SQL, cn);

var definition = rs(2).value;

//document.write("document.getElementByID('boldStuff3').innerHTML ="definition)
document.getElementsById('boldStuff3').innerHTML = definition;
document.getElementsById('userText').value = definition;

 

Unfortunately, The innerHTML is not working. If I do a document.write(definition), it outputs the proper information, but changing it inline is not working at all.

 

Please let me know.

 

Thanks,

DaVuLF

Link to comment
Share on other sites

hi try this function to get the variable in the url

function getQueryVariable(variable) 
{ 
var query = window.location.search.substring(1); 
var vars = query.split("&"); 
for (var i=0;i<vars.length;i++) 
{ 
var pair = vars[i].split("="); 
if (pair[0] == variable) 
{ 
return pair[1]; 
} 
} 
} 

Link to comment
Share on other sites

Hi DJ Kat,

 

That didn't quite seem to work out. I will post the entire code that I am currently using, as that may help a little more. Essentially what I am trying to do is to have a select dropdown populated by values in a database, and then upon selecting one of those, to have the associated text (ie, the column in the database beside the name) to appear in a textarea below the dropdown.

 

Here is the code:

 

<html>
<head>
  <title>Untitled Document</title>

<script language="JavaScript">

function getQueryVariable(variable) 
{ 
var query = window.location.search.substring(1); 
var vars = query.split("&"); 
for (var i=0;i<vars.length;i++) 
{ 
var pair = vars[i].split("="); 
if (pair[0] == variable) 
{ 
return pair[1]; 
} 
} 
}

function changeText(){
var userInput = document.getElementById('testinput').value;
document.getElementById('boldStuff2').innerHTML = userInput;

var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\PCL\\test.mdb;Persist Security Info=False";
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "select * from tablename WHERE term_name='"+userInput+"'";
rs.Open(SQL, cn);

var definition = rs(2).value;
document.write(definition);

//document.write("document.getElementByID('boldStuff3').innerHTML ="definition)
document.getElementsById('boldStuff3').innerHTML = definition;
document.getElementsById('userText').value = definition;

document.write(name);

rs.Close();
cn.Close(); 
}

//Initiate the connection to the database.
var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\PCL\\test.mdb;Persist Security Info=False";
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "select * from tablename";
rs.Open(SQL, cn);
   
document.write("<form name='testform' action='test3.html' method='get'>");
document.write("<select id='testinput' name='testinput'>");

while(!rs.eof) {

document.write("<option value='"+rs(1)+"'>"+rs(1)+"</option>");

document.write(rs(1) + "<br>");
rs.MoveNext()
}

document.write("</select>");
document.write("<input type='submit' value='Show'>");
document.write("</form>");

rs.Close();
cn.Close(); 

var name = getQueryVariable("testinput");
name.replace(/\+/g," ");
document.getElementsById('boldStuff3').innerHTML = name;

</script>

</head>
<body>

<p>Welcome to the site <b id='boldStuff2'>dude</b> </p> 
<p>Welcome to the site <b id='boldStuff3'>dude</b> </p> 
<p><textarea id='userText'></textarea></p>

<input type='button' onclick='changeText()' value='Change Text'/>
</body>
</html>

 

Thanks for the help, I appreciate it!

Link to comment
Share on other sites

Okay, so I have tried another approach found at: http://www.w3schools.com/ajax/ajax_database.asp

 

Here is the HTML: (dictionary.html)

 

<html>
<head>
<script src="selectterm.js"></script>
</head>
<body>
<form> 
Select a term:
<select name="terms" onchange="showTerm(this.value)">
<option value="Client">Client
<option value="NORTS ">North/South
<option value="WOLZA">Wolski Zajazd 
</select>
</form>
<p>
<div id="txtHint"><b>term info will be listed here.</b></div>
</p>
</body>
</html>

 

Here is the JS: (selectterm.js)

 

var xmlHttp

function showTerm(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="getterm.asp";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

 

Here is the ASP: (getterm.asp)

 

<%
response.expires=-1
sql="SELECT * FROM tablename WHERE term_name="
sql=sql & "'" & request.querystring("q") & "'"

set cn = new ActiveXObject("ADODB.Connection");
set strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\PCL\\test.mdb;Persist Security Info=False";
cn.Open(strConn);
set rs = Server.CreateObject("ADODB.recordset")
rs.Open sql, conn

response.write("<table>")
do until rs.EOF
  for each x in rs.Fields
    response.write("<tr><td><b>" & x.term_name & "</b></td>")
    response.write("<td>" & x.term_text & "</td></tr>")
  next
  rs.MoveNext
loop

response.write("</table>")
%>

 

I changed the values in the tutorial for my values, but it isn't working either in Opera, or IE7. In Opera it outputs the ASP code without any variables filled in, in IE7 it does nothing. I didn't really change anything aside from what I was looking for, and yet it isn't working.

 

Thanks for any help! :)

Link to comment
Share on other sites

After doing some digging, it seems like ASP requires a server to be running on the host computer. Considering this is supposed to be run without a server (only a database), I don't think the ASP will work (although correct me if I'm wrong). As a result, is there any way to code the ASP there into JavaScript?

 

Thanks,

DaVuLF

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.