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


How to show results of a form upon submission

使用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>

触发提交按钮后,在同一页面上填充结果的最佳方式是什么,即"67%的用户是女性"answers"30%的用户骑自行车"?

尝试以下方法:

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>
<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;

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

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

表单页面:

<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>

表单处理器.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,如果你使用了,我可以调整代码。我刚刚写了代码,所以我不确定它是否有效,但给你!