0

使用 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% 的用户是女性”和“30% 骑自行车”?

4

3 回答 3

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;

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

于 2013-07-23T20:59:01.307 回答
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>
于 2013-07-23T20:12:43.200 回答
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,如果是,我可以调整代码。我刚刚写了代码,所以我不确定它是否有效,但是给你!

于 2013-07-23T21:33:54.690 回答