将HTML表单提交到外部asp脚本

时间:2014-03-27 22:19:26

标签: javascript html asp.net

我正在尝试编写一个html表单,将一些变量提交给创建聊天窗口的外部asp文件。

我提供的示例是http://na.ntrsupport.com/nv/WebIntegration/Connectwise/contactform.aspx?account=41403&lang=us&banner=0&button=connect,但这需要两个步骤。

在提供的示例中,客户填写字段,点击“提交”,然后转到单独的页面,点击动态生成的JavaScript链接以启动聊天窗口。

我希望在单个页面上执行此操作,但我无法找到将变量从HTML表单正确传递到外部脚本的方法

<script language='JavaScript' src='https://na.ntrsupport.com/nv/inquiero/web/an/ann4.asp?login=41403&lang=us&button=connect&cat=&cob=&oper=&skin=&urloffline=&urlbusy=&sur=&surpre=&bgcolor=&txtcolor=&ref2=/o1https://na.ntrsupport.com/nv/Webintegration/Connectwise/entrypoint.asp?p=1;email%40address.com;Description;Customers+Name;Company+Name&ref=Customers+Name&clientid=Company+Name'> </script>

在上面的代码中,我在“公司名称”和“email@address.com”等字段中输入了通用文本,以获取该示例链接。

我尝试过多种方法,但没有一种方法可以达到预期的效果,例如使用javascript直接使用表单中的变量来管理url,以及在html表单的action部分使用url。

我无法控制na.ntrsupport.com上的脚本如何工作或接收请求,因此我必须从头到尾完成此工作。

任何帮助都会受到赞赏,如果我错过任何重要的事情,请告诉我。

到目前为止我的代码

<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<script>
function launchChat()
{
  var name = document.getElementById('name').value;
  var company = document.getElementById('company').value;
  var email = document.getElementById('email').value;
  var description = document.getElementById('description').value;

  var url = "https://na.ntrsupport.com/nv/inquiero/web/an/ann4.asp?login=41403&lang=us&button=connect&cat=&cob=&oper=&skin=&urloffline=&urlbusy=&sur=&surpre=&bgcolor=&txtcolor=&ref2=/o1https://na.ntrsupport.com/nv/Webintegration/Connectwise/entrypoint.asp?p=1;" + email + ";" + description + ";" + name + ";" + company + "&ref=" + name + "&clientid=" + company;

window.location(url);
return false;
}
</script>
<style>
div{
    width:400px;
    height:200px;
    position:absolute;
    left:50%;
    top:50%;
    margin-left:-100px; /* Negative half the width*/
    margin-top:-100px; /* Negative half the height */
}
</style>
</head>
<body>
<div>
<form onsubmit="return launchChat();">
<table>
<tr>
<td colspan=2>
Put Banner here
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<input type="text" id=="name" name="name" required>
</td>
</tr>
<tr>
<td>
Company:
</td>
<td>
<input type="text" id="company" name="company" required>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="email" id=name="email" name="email" required>
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
<input type="text" id="description" name="description" required>
</td>
</tr>
<tr>
<td colspan=2>

<input type="submit" id="submit" name="submit" value="Start Chatting"/>
</td>
</tr>
</table>
</form>
</div>
</html>

1 个答案:

答案 0 :(得分:0)

好吧,所以你在那里做得太复杂了。

第一件事:你需要使用encodeURIComponent(这是关键!)那些你正在连接到字符串的变量,因为它们中的一些可能会在url中包含字符。

第二:在表单上使用GET方法会自动构建这样的URL。例如:

<form action="a.html" method="GET">
    <input name="var1" value="value1" />
    <input name="var1" value="value1" />
</form>

会自动构建如下所示的链接:a.html?var1=value1&var2=value2,这是您需要的一部分,可以让您的生活更轻松。我知道你基本上需要构建其中的两个(主要的url,我不确定它将保持硬编码;以及ref2部分)。


另一件事:

window.location(url);

应该是:

window.location = url;
相关问题