我有以下由一些 PHP 逻辑呈现的表单。表格渲染得很好;你可以看到文本输入和提交按钮等等。
在 IE 中,表单按预期工作。第一个表单转到“index.php?subscribe=go”,第二个表单转到“index.php?unsub=go”,但在 FF 和 Chrome 中,单击提交按钮会重新加载页面(不转到表单操作)。我没有检查其他浏览器。
我在 Firebug 中发现该标签甚至在 Firefox 的页面上<form>
都不存在。这很奇怪;看看这个:
else
{
echo '<div class="subs_main">';
if (isset($_GET['subscribe']))
{
if ($_GET['subscribe'] != 'go')
{?>
Subscribe to <b>Bella Blog</b> for specials, sales, news and more!
<br />
<form action="index.php?subscribe=go" method="post" name="subscribe_form" onsubmit="return checkForm();">
Name: <input type="text" name="name" size="15" />
<br />
Email: <input type="email" name="email" size="20" />
<br />
<input type="submit" value="subscribe!" name="submit" />
</form>
<p class="unsub">You can <a href="index.php?unsub">unsubscribe</a> at any time</p>
<?php
}
else
{
// subscribe user
}
}
elseif (isset($_GET['unsub']))
{
if ($_GET['unsub'] != 'go')
{?>
Sorry to see you go! You can <a href="index.php?subscribe">re-subscribe</a> at any time!
<br />
<form onsubmit="return checkForm2()" name="unsub_form" method="post" action="index.php?unsub=go">
Email: <input type="email" name="email" size="20" />
<br />
<input type="submit" value="unsubscribe" name="submit" />
</form>
<?php
}
else
{
// process unsubscription HERE
}
}
echo '</div>';
}
这是用于表单验证的 JS(我认为可以忽略不计,因为它在 IE 中工作,并且在注释掉这个脚本时得到相同的结果):
<script type="text/javascript" language="javascript">
function checkForm()
{
var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
var form = document.forms.subscribe_form;
var name = form.name.value;
var email = form.email.value;
if (name == '' || email == '')
{
alert('You must enter both your name and email address!');
return false;
}
else if (!email.match(regex))
{
alert('You must enter a valid email!');
return false;
}
return true;
}
function checkForm2()
{
var form = document.forms.unsub_form;
var email = form.email.value;
if (email == '')
{
alert('You must enter an email address!');
return false;
}
return true;
}
</script>