使用Ajax和传统的ASP查询数据库 [英] Query database with Ajax and classic ASP

查看:69
本文介绍了使用Ajax和传统的ASP查询数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图修改从w3scools脚本中使用ASP和AJAX的组合来查询数据库,并返回结果。

I'm trying to modify a script from w3scools to use a combination of asp and ajax to query a database and return results.

下面是code:

<html>
<head>
<script type="text/javascript">
function showCustomer(str)
{
    var xmlhttp;    
    if (str=="")
    {
        document.getElementById("txtHint").innerHTML="";
        return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","getcustomer.asp?q="+str,true);
    xmlhttp.send();
}
</script>
</head>
<body>

<form action=""> 
    <select name="customers" onchange="showCustomer(this.value)">
        <option value="">Select a customer:</option>
        <option value="ALFKI">Alfreds Futterkiste</option>
    </select>
</form>
<br />

<div id="txtHint">Customer info will be listed here...</div>

</body>
</html>

我想用2输入字段替换选择字段。

I would like to replace the select field with 2 input fields.

有人能告诉我如何修改的JavaScript,使这两个输入值传递的查询字符串,需要什么样的形式更改为调用该函数。

Can someone please tell me how to amend the javascript so that both the input values are passed with the querystring and what needs to be changed on the form to call the function.

感谢

推荐答案

这取决于正是你想要怎样的页面进行操作。你能成为一个更具体一点?

It depends exactly how you want the page to operate. Can you be a bit more specific?

当你说'输入值你的意思是一个文本框?

When you say 'input values' do you mean a text box?

我假设在下面的例子中,你将有两个字段和提交按钮:

I'm assuming in the following example that you'll have two fields and a button for submitting:

<form action=""> 
    <label for="MyTextBox1>Enter some text:</label>
    <input type="text" id="MyTextBox1" />

    <label for="MyTextBox1>Enter some text:</label>
    <input type="text" id="MyTextBox2" />

    <input type="button" onclick="showCustomer();" />
</form>

你的JavaScript函数的定义将改变,从

The definition of your JavaScript function will change from

function showCustomer(str)

function showCustomer()

和你需要删除任何相关的 STR code。

And you'll need to remove any associated str code.

要拿起这些值使用的document.getElementById

var val1 = document.getElementById("MyTextBox1").value);
var val2 = document.getElementById("MyTextBox1").value);
xmlhttp.open("GET","getcustomer.asp?q="+ val1 +"&r=" + val2 ,true);

这是非常粗糙,准备好了,但一个很好的起点。

This is very rough and ready, but a good starting point.

这篇关于使用Ajax和传统的ASP查询数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆