如何在提交时显示表单的结果

时间:2013-07-23 20:07:17

标签: php javascript html5

使用WordPress,PHP,HTML,CSS,JavaScript提交表单结果时填写表单的最佳方法是什么?我可以使用ddl,单选按钮等表单。

<form>
     <input type="radio" name="sex" value="male">Male<br>
     <input type="radio" name="sex" value="female">Female

     <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
     <input type="checkbox" name="vehicle" value="Car">I have a car 

     <input type="submit" value="Submit">
</form>

填充结果的最佳方法是什么,即&#34; 67%的用户是女性&#34;和#34; 30%骑自行车&#34;一旦触发提交按钮,在同一页面上?

3 个答案:

答案 0 :(得分:0)

尝试以下几点:

script.php
----------
<html>
<body>
<form method=post action=script.php>
   your form here...
</form>

<? if($_SERVER["CONTENT_LENGTH"]>0) {
     //form was submitted to script, so process form inputs here and display results...
  }
?>
</body>
</html>

答案 1 :(得分:0)

<form action="phpfile.php" method="Post">
...form here
</form>

phpfile.php:

<?php
if(isset($_POST['sex'])){
$sex = $_POST['sex'];
}//etc..
//To show the result, simply echo it:
echo $sex;

您需要某种存储系统才能计算出每种存储系统的数量。 所以你需要做的是写一个简单的查询,其中字段的值是男性或女性。然后你可以很容易地计算它。

答案 2 :(得分:0)

您需要做的是将表单结果添加到数据库,然后查找两个类别的结果总数,然后计算女性百分比和自行车百分比。

表单页面:

<html>
   <?php
      if(isset($_GET['status']) && $_GET['status'] == "values") {
          //connect to DB and select table
          $male = count( mysql_query("SELECT gender FROM Table WHERE gender='male'"));
          $female = count (mysql_query("SELECT gender FROM Table WHERE gender='female'"));
          $car = count (mysql_query("SELECT vehicle FROM Table WEHRE vehicle='car'"));
          $bike = count (mysql_query("SELECT vehicle FROM Table WEHRE vehicle='bike'"));
          $totalResults = $male + $female;
          $totalVehicles = $car + $bike;

          $percentFemale = 100 * ($female / $totalResults);
          $percentFemale = number_format($percentFemale, 0);

          $percentBike = 100 * ($bike / $totalVehicals);
          $percentBike = number_format($totalBike, 0);

          echo "<p>" . $percentFemale . "% of users are female. </p>";
          echo "<p>" . $percentBike . "% of users use bikes. </p>";
      } else { ?>
         <form action="formProcessor.php" method="POST"><!-- You could also use GET -->
           <input type="radio" name="sex" value="male">Male<br>
           <input type="radio" name="sex" value="female">Female

           <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
           <input type="checkbox" name="vehicle" value="Car">I have a car 

          <input type="submit" value="Submit">
        </form>
     <?php } ?>
</html>

formProcessor.php页面:

<?php
   if(isset($_POST["sex"]) && isset($_POST['vehicle'])) { 
     //Connect to MySQL DB and select table here
     $sex = $_POST['sex'];
     $vehicle = $_POST['vehicle'];
     mysql_query("INSERT INTO Table(sex, vehicle) VALUES($sex, $vehicle)"); //You may also want to add an id column, but...
     //Close mysql connection
     header("Location: /form.php?status=values");
     die();
   } else {
     die("There was an error in your submission.");
   }
?>

我认为这回答了你的问题,你有办法找到自行车上女性用户和用户的百分比。如果您需要它是动态的并且仅显示更大的数量(或显示两者或某事),只需添加注释即可。这也假设你没有使用PDO,如果你是,我可以调整代码。我刚刚写了代码,所以我不确定它是否有效,但是你去了!