【发布时间】:2016-08-04 19:04:44
【问题描述】:
我一直在用 PHP 开发一个非常简单的邮件表单。我在 div 元素中有每个问题,这个 javascript 揭示了下一个 div/问题(它在文件 form.php 中)
<script type="text/javascript">
function hideshow(which){
if (!document.getElementById)
return if (which.style.display=="block")
which.style.display="none"
else which.style.display="block" }
</script>
并且一个单独的代码在显示下一个元素之前验证选择或输入的内容。
好的,所以在索引页面上一切正常。但是,在联系我们页面上(我希望该表格可用,因为我怀疑很多人不会填写索引页面上的弹出窗口)我遇到了障碍。我的脚本一直前进到最后一个问题和提交表单的按钮。然后什么也没有发生...我可以单击该按钮,但它没有提交表单。这是应该提交表单的脚本(包含在 validation.js 中):
function ageValidate(){ 'use strict';
//Verfiy that submitter is not a robot
var age=document.getElementById('age').value;
if(age === ""){ // no answer, submit form
document.getElementById('ageErr').innerHTML = 'This is to validate that you are not a robot, please enter the correct answer to the problem.';
return false;
} else if (age === "5"){ // right answer
submitForm(); return true; } else { // wrong answer
document.getElementById('ageErr').innerHTML = 'Your answer is incorrect or you did not use the numeric format. 2 is valid, but two is not valid.'; } }
$(document).ready(function() { $(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
} }); });
function submitForm() { if (document.getElementsByName("serviceType")[0].value === "")
return false; else if (document.getElementsByName("numberPhones")[0].value === "")
return false; else if (document.getElementsByName("zip")[0].value === "")
return false; else if (document.getElementsByName("email")[0].value === "")
return false; else if (document.getElementsByName("name")[0].value === "")
return false; else if (document.getElementsByName("age")[0].value === "")
return false; else { document.forms["howHelp"].submit(); }
以及form.php中表格的最后一个问题:
<div id="q7" style="display:none">
<div class="how-help">
<h2>What is eight minus three?</h2>
<center><div>
<input type="text" name="age" id="age" value="<?php echo $age;?>">
</div><br/>
<div id="ageErr" name="ageErr" style="color: #000; background: #41b0cc; font-weight:700; width: 400px; height: auto; margin:0px; font-size:10pt;"></div></center>
</div>
<!-- FOOTER -->
<div class="how-help-footer">
<center>
<input type="button" class="jump"
value="Continue"
onclick="ageValidate()"></form>
</center><a href="javascript:hideshow(document.getElementById('q6')); hideshow(document.getElementById('q7'))" class="back">BACK</a><div class="private"><a href="#" class="private"> Privacy Policy</a></div>
</div>
</div>
<script src="validation.js" language="javascript" type="text/javascript"></script>
同样,它在索引页面上工作得非常好,但在联系页面上却不行...... 我应该补充一点,所有文件都在同一个目录中,所以指向错误的目录应该没有任何问题。我用
php 包含'form.php'
在两个地方都有表单,我不确定这是否是问题,但我的表单操作是:
$_SERVER['PHP_SELF'];
我应该补充一点,这是一个由非常乐于助人的人组成的了不起的社区,如果没有提出和回答很多问题,我就不会在这个项目中处于我的位置,所以感谢过去和提前所做的一切!
【问题讨论】:
-
确保您在 index.php 和 contact.php 页面上具有相同的 ID,因为您对两者使用相同的脚本。还要确保您的contact.php 页面上没有任何冲突的ID。您可能还应该在此处包含您的
<form>代码。 -
请完整包含
<form>。此外,请准确说明您是如何包含form.php的,因为php include 'form.php'按原样实际上不会包含该表单。 -
这是我的第一篇文章,我正在研究如何添加代码,因此缺少部分;但是它在页面上是:
<?php include 'form.php' ?> -
@Bruno 你完全正确。我在该页面上的联系表格有一些相同的变量。我什至没有想到这一点。谢谢!
-
很高兴能帮上忙!
标签: javascript php jquery forms submit