0

我有一个 HTML 文件、JavaScript 文件和一个 PHP 文件。JavaScript 用于验证用户输入,然后将表单传递到 PHP 页面。

验证功能运行良好,但表单不会从 JavaScript 提交到 PHP 文件。

在花了两天时间之后,我仍然无法弄清楚我有什么问题以及为什么 JavaScript 不会将表单数据提交到 PHP 文件中以及如何在提交后出现 PHP 页面

HTML:

<div class = "formtext">
<p>
<form id = "formtext" name="survey" method="post" action="formtext.php">
<table id="surveytable" width="300" border="1">
<tr>
<td>
    Name: 
        <input type = "text" id="userName" 
name="name" onkeyup="document.getElementById('mirror').innerHTML=this.value" />
    <br/ >
    Your name: <span id="mirror"></span>
</td>
</tr>

<tr>
<td>
    Number: 
        <input type = "text" id="userNumber" 
name="number" onkeyup="document.getElementById('mirror1').innerHTML=this.value" />
    <br />
    Your number: <span id="mirror1"></span>
</td>
</tr>

<tr>
<td>
    Email: 
        <input type = "text" id="userEmail" 
name="email" onkeyup="document.getElementById('mirror2').innerHTML=this.value" />
    <br />
    Your email: <span id="mirror2"></span>
</td>
</tr>

<tr>
<td>
    Gender:<br />
    <input type="radio" name="gen" id="userGender" value="Decline"
checked/> Decline<br />
        <input type="radio" name="gen" id="userGender" value="Male" />Male<br />
        <input type="radio" name="gen" id="userGender" value="Female" /> Female

<tr>
<td>
    Major:
        <select name="major" id="userMajor">
    <option value="" selected="selected">Select Major</option>
        <option value="Computer Science">Computer Science</option>
        <option value="Information Technology">Information Technology</option>
        <option value="Engineering">Engineering</option>
        <option value="Biology">Biology</option>
    <option value="Other">Other</option>
        </select>
</td>
</tr>

<tr>
<td>
    Hobbies:<br /> 
        <input name="hob" id="userHobby" type="checkbox" value="Running, 
" + " " />Running<br /> 
        <input name="hob" id="userHobby" type="checkbox" value="Sleeping, 
" + " "/>Sleeping<br />
    <input name="hob" id="userHobby" type="checkbox" value="Fishing, 
" + " "/>Fishing<br />
    <input name="hob" id="userHobby" type="checkbox" value="Other" />Other
</td>
</tr>
</table>
        <p>
        <input type="button" value="Enter" onclick="Survey()"/>
    </p>
    </form>

    </div>
</form>

JavaScript:

function Survey()
{
if (!ValidateForm())
{        
return false;
}


else {
var userName = document.forms[0].name.value+"<br />";

var userNumber = document.forms[0].number.value+"<br />";

var userEmail= document.forms[0].email.value+"<br />";

var sex;
  if (document.forms[0].gen[0].checked)
    sex=document.forms[0].gen[0].value;
  else if (document.forms[0].gen[1].checked)
    sex=document.forms[0].gen[1].value;
  else if (document.forms[0].gen[2].checked)
    sex=document.forms[0].gen[2].value;

var userGender = sex+"<br />";

var userMajor = document.forms[0].major.value+"<br />"; 

var hobby=new Array()
var counter=0;

  for (i=0;i<document.forms[0].hob.length;i++)
  {
    if (document.forms[0].hob[i].checked)
    {
        hobby[counter]=document.forms[0].hob[i].value;
        counter++;
    }

  }

var hobPick="";
for (n=0;n<hobby.length;n++)
    hobPick+=hobby[n];

var userHobby = hobPick;

document.getElementById("printName").innerHTML = userName
document.getElementById("printNumber").innerHTML = userNumber
document.getElementById("printEmail").innerHTML = userEmail
document.getElementById("printGender").innerHTML = userGender
document.getElementById("printMajor").innerHTML = userMajor
document.getElementById("printHobby").innerHTML = userHobby


document.getElementById('formtext').submit();
}
}

function ValidateForm()

{

if (document.forms[0].name.value.length === 0) 
{
alert("Name is Required");

    return false;

}

else if
(document.forms[0].number.value.length === 0) 
{
alert("Number is Required");

    return false;
}

else if
(document.forms[0].email.value.length === 0) 
{
alert("Email is Required");

    return false;
}

else if
(document.forms[0].userMajor.value === "")
{
    alert("Major is Required");

        return false;
}

else if
(document.forms[0].userHobby[0].checked === false && 
document.forms[0].userHobby[1].checked=== false &&
document.forms[0].userHobby[2].checked === false &&
document.forms[0].userHobby[3].checked === false)
{
    alert("hobby is Required");

        return false;
}
else
{   
return true;
}
}

PHP:

<table id='userInputTbl' width='600' border='1'>

<tr> 
<td id='tblName' colspan='6'> 
<strong>You Submitted: </strong><br />
</td>
</tr>

<tr>
<td><strong>Name:</strong><br />
</td>
<td> <?echo $_POST["printName"]?></td>
</tr>

<tr>
<td><strong>Number:</strong><br /></td>
<td> <?echo $_POST['printNumber']?></td>
</tr>

<tr>
<td><strong>Email:</strong><br /></td>
<td> <?echo $_POST['printEmail']?></td>
</tr>

<tr>
<td><strong>Gender:</strong><br /></td>
<td> <? echo $_POST['printGender']?></td>
</tr>

<tr>
<td><strong>Major:</strong><br /></td>
<td> <? echo $_POST['printMajor']?></td>
</tr>

<tr>
<td><strong>Hobbies:</strong><br /></td>
<td> <? echo $_POST['printHobby']?></td>
</tr>

</table>
4

4 回答 4

1

您的问题在于这部分:

document.getElementById("printName").innerHTML = userName
document.getElementById("printNumber").innerHTML = userNumber
document.getElementById("printEmail").innerHTML = userEmail
document.getElementById("printGender").innerHTML = userGender
document.getElementById("printMajor").innerHTML = userMajor
document.getElementById("printHobby").innerHTML = userHobby

不存在这样的 ID!顺便说一句,ID 应该是唯一的,这意味着只有一个元素应该具有userHobby 作为 ID。

<td>
    Hobbies:<br /> 
        <input name="hob" id="userHobby" type="checkbox" value="Running, 
" + " " />Running<br /> 
        <input name="hob" id="userHobby" type="checkbox" value="Sleeping, 
" + " "/>Sleeping<br />
    <input name="hob" id="userHobby" type="checkbox" value="Fishing, 
" + " "/>Fishing<br />
    <input name="hob" id="userHobby" type="checkbox" value="Other" />Other
</td>
于 2013-09-24T19:58:37.747 回答
1

您的ValidateForm()函数中似乎有一个额外{的:

{
var userName = document.forms[0].name.value+"<br />";

也许您想将其更改为:

else {
var userName = document.forms[0].name.value+"<br />";
于 2013-09-24T19:59:20.787 回答
0

您的 php 文件已损坏。

<?php
echo <table id='userInputTbl' width='600' border='1'>
<tr> 
 <td id='tblName' colspan='6'> 
 <strong>You Submitted: </strong><br />
 </td>
 </tr>
 <tr>
   <td><strong>Name:</strong><br />
 </td>
   <td <?echo $_POST["userName"]?> ></td>

echo需要一个字符串,但无论如何您都不需要任何字符串。

摆脱开口<?php和回声。像这样直接进入html

<table id='userInputTbl' width='600' border='1'>
<tr> 
 <td id='tblName' colspan='6'> 
 <strong>You Submitted: </strong><br />
 </td>
 </tr>
 <tr>
   <td><strong>Name:</strong><br />
 </td>
   <td <?echo $_POST["userName"]?> ></td>

这部分看起来也有问题:

   <td <?echo $_POST["userName"]?> ></td>

我想你的意思是

   <td><?echo $_POST["userName"]?></td>

此外,在本节中:

function Survey()
{
if (!ValidateForm())
{        
return false;
}


{
var userName = document.forms[0].name.value+"<br />";

{是不合适的。你的意思是有一个else {那里?


还有一件事:如果您只是提交表单,那么向元素显示值有什么意义id="print..."?用户将看不到任何内容,只是在他们被重定向之前的一瞬间。摆脱这一切。


我又回来了...

你在这里不一致:

document.forms[0].hob[i]

与这个:

document.forms[0].userHobby[0]

您应该在这里使用名称,而不是 ID。使用hob.

于 2013-09-24T19:58:54.040 回答
-2

您必须在表单的 OnSubmit 事件中进行此验证,请参阅W3Schools 中的这篇文章

于 2013-09-24T20:01:02.130 回答