使用PHP显示用户输入

时间:2016-03-26 17:56:53

标签: javascript php html html5

我要做的是使用PHP在单独的页面上显示用户输入,但是当我处理表单时,不会显示任何输入值。我必须使用javascript进行输入验证,并且工作正常,但PHP页面无效。 这是我的代码: HTML和Javascript

survey.php:

  <!DOCTYPE html>
  <html>

  <head>
    <title>Survey Page</title>
    <link rel="stylesheet" type="text/css" href="mystylepage.css" />

    <script type="text/javascript">
    function validate()
    {

        if (document.information.name.value == "" )
        {
            alert("Please provide your name!" );
            document.information.value.focus();
            return false;
        }
        if (document.information.email.value == "")
        {
            alert("Please provide an email!");
            document.information.value.focus();
            return false;
        }
        if (document.information.major.value == false)
            {
                alert("Please enter your major");
                document.information.value.focus();
                return false;
            }
        return true;
    }

    </script>
  </head>

  <div id="bodycolor">

  <body>

  <div id="container">
  <div id="menu">Menu: 

  <a class="menu" href="index.htm">Home</a> | <a class="menu"                                          
   `href="faculty.htm">Faculty</a> | 
   <a class="menu" href="courses.htm">
  Courses</a> | <a class="gradecalculator" href="gradecalculator.htm">Grade      
  Calculator</a> | <a class="survey" href="survey.htm">Survey</a>         

  </div>

  </div>

  <div id="header">

  <h1><center>CSE Center for Health Information Technology</center></h1>

  <br>

  <div id="links">

  <center><a href="https://www.kennesaw.edu/"><img src="KSU Logo.jpg"      
   alt="Logo" style="width:100px;height:100px;" /></a></center>

  <br>

   <center><a href="http://ccse.kennesaw.edu/">College of Computing and   
   Software Engineering</a></center>

  </div>

  </div>

  <h1>Please enter you information below</h1>
  <form name="information" method="POST" action="action.php"    
  onsubmit="return     
   validate();">
    <table width="500" border="0">
    <tr>
    <td width="150" bgcolor="#99CCFF"><strong>Name</strong></td>
    <td><input type="text" name="name" size="20" maxlength="20" /></td>
    </tr>
    <tr>
    <td bgcolor="#99CCFF"><strong>Email</strong></td>
  <td><input type="text" name="email" size="20" maxlength="20" /></td>
  </table>
  <p> Major:
    <input name="major" type="radio" value="Information Technology" />
    Information Technology
    <input name="major" type="radio" value="Software Engineering" />
    Software Engineering
    <input name="major" type="radio" value="Computer Science" />
    Computer Science</p>
  <p>
  <input type="submit" value="Process" />
  </p>
   <p id="msg"></p>
  </form>

 </body>

</html>

这是PHP页面:

action.php的

 <!DOCTYPE html>
<html>
<head>
    <title>Survey Information Processing</title>
</head>

<body>

<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]?></p>

<p>Email: <?php echo $_POST["email"]?</p>

<p>Major: <?php echo $_POST["major"]?</p>


</body>

</html>

我是PHP的新手,所以非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

所以问题是您的方法已经获得,但您正试图通过帖子

进行访问

更改

method="GET" 

method="POST"

在表格上。

答案 1 :(得分:0)

您的表单方法应该是POST。

 <form name="information" method="POST" action="action.php" onsubmit="return validate();">

并在提交按钮中添加名称:

<input type="submit" value="Process" name="submit" />

此外,您可以添加以下条件:

<?php 
  echo "<pre>";
  print_r($_POST);   // Check if you're getting any POST data
  echo "</pre>";
?>

<?php if (isset($_POST['name'])) { ?>

 <h1>Form data</h1>
 <p>Name: <?php echo $_POST["name"]; ?> 
 </p>
 <p>Email: <?php echo $_POST["email"]; ?> 
 </p>
 <p>Major: <?php echo $_POST["major"]; ?> 
 </p>

<?php } ?>

希望这有帮助。