一个 ASP.NET 页面中不能有两个表单,并且两者都在代码隐藏中工作。
您拥有的替代方案。
- 将第二个窗体放在 asp.net 窗体之前或之后
- 不要设置第二种形式,只需在后面的代码中读取您感兴趣的输入数据。
- 使用 javascript 在页面末尾动态创建表单并发布。
因为在这里我看到你发布到 php 并且我认为你喜欢用 asp.net 保留旧代码,add them after the closing form of the asp.net form.
<form id="AspForm" runat="server">
...
</form>
<form id="ajax-contact-form" method="post" action="contact.php" >
...
</form>
更极端
因为你们都已经准备好使用 javascript,所以您可以做到这一点。将所有内容复制并粘贴到页面末尾的表单中并发布。
<form id="AspForm" runat="server">
...
<div id="ajax-contact-infos" >
<label>
Name:</label><input class="textbox" name="name" value="" type="text">
<label>
<input class="pin" name="submit" value="Submit"
type="button" onclick="SubmitThisForm();return false;" >
...
</div>
</form>
<form id="ajax-contact-form" method="post" action="contact.php" >
<div id="PlaceOnMe" ></div>
</form>
并在 javascript 上将其复制,然后将其发布为
<script type="text/javascript">
function SubmitThisForm()
{
// here I copy (doublicate) the content of the data that I like to post
// in the form at the end of the page and outside the asp.net
jQuery("#PlaceOnMe").html(jQuery("#ajax-contact-infos"));
// Now we submit it as its your old code, with out change anything
jQuery("#ajax-contact-form").submit();
}
// here is the old code.
$(document).ready(function () {
$("#ajax-contact-form").submit(function () {
var str = $(this).serialize(); $.ajax({ type: "POST", url: "contact.php", data: str, success: function (msg) {
if (msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
{ result = '<div class="notification_ok">Your message has been sent. Thank you!<br /> <a href="#" onclick="freset();return false;">send another mail</a></div>'; $("#fields").hide(); } else
{ result = msg; } $("#note").html(result);
}
}); return false;
});
});
function freset()
{ $("#note").html(''); document.getElementById('ajax-contact-form').reset(); $("#fields").show(); }
</script>
这一切都是通过最小的改变实现的。你可以如何进行优化,但这是一般的想法。