duanlvji4780 2014-05-01 04:03
浏览 77
已采纳

使用PHP从HTML表单中收集信息

This is what I got.

formulario.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">

<html>
<head>
<title> Surveys </title>
</head>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<body>
<form action="main2.php" method="post">

    Number of Surveys <input type="number" name="number">
    <input type="submit" value="Send"/>

</form>
</body>
</html>

main2.php

<html>
<body>

<?

function Survey($number){

    if($number > 0){ 
        $a = 1;
        while ($a <= $number){ ?>
<h2>Survey for person number <? echo $a;?></h2> 
<form action="resultados.php" method="post">
Age <input type="number" name="age"/> <br><br>
Genre <br><br>
<input type="radio" name="male" value="6"/> Male <br>
<input type="radio" name="female" value="7"/> Female <br><br>
Marital Status <br><br>
<input type="radio" name="single" value="1"/> Single <br>
<input type="radio" name="married" value="2"/> Married <br>
<input type="radio" name="divorced" value="3"/> Divorced <br>
<input type="radio" name="free" value="4"/> Free Union <br>
<input type="radio" name="widowed" value="5"/> Widowed <br><br>
<?          
            $a = $a + 1;
        }
    }
}
echo Survey($_POST['number']);


?>

<input type="submit" value="Send"/>
</form>
</body>
</html>

resultados.php

<?

function Calculate($age){
    $array_of_ages = array();
    $array_of_ages[] = $age;
    var_dump($array_of_ages);
}

echo Calcular($_POST['age']);
?>

I want to create a little survey, so in the "formulario.php" file I select an amount of people to go on "main2.php" and create the survey for each. For example, if I select 3 persons, what "main2.php" code should do is to create 3 surveys, one after another, in the same page with only a "submit" buttom. Then, when I "fill all the surveys and press the submit buttom what I'm trying to do with "resultados.php" is to get all the data gathered from those forms and do some stuff like, counting how many people is married or divorced, an average of age in women and men, etc... and show them, after I press "Send".

The problem is that I'm stuck and I don't have any clue on how to do it. I've searched about in ways to catch the info like, arrays, loops, but none of it works. As you can see, I created "resultados.php" where I would supposely show the info processed but no luck.

Depending on which number I select in the first .php file, the "while" in the "main2.php" creates the amount of surveys suggested, but I don't know if it's the right way to do it, according for what I want to reach.

I'll appreciate any help on this. I just started to learn PHP this week and I'm frustrated now because I can't figure out how to solve it by my own.

PD: Sorry for my english, not my native language.

  • 写回答

1条回答

  • dongyoudi1342 2014-05-01 04:50
    关注

    The first important thing is, your <form> tag is inside the while loop. Though your form is closing only once, it's opening several times which results in an invalid HTML. So place it before the while loop this way:

    $a = 1; ?>
    <form action="resultados.php" method="post">
    <? while ($a <= $number){ ?>
    <h2>Survey for person number <? echo $a;?></h2>
    

    Second thing, since you have age repeating multiple times in your form, you should make it as an HTML input array, only then it can hold multiple values, so change this your age input's name attribute to:

    <!-- `[]` tells the HTML that all values of input with this name should
    be preserved. If you do not have this, only your last value will be passed -->
    <input type="number" name="age[]"/>
    

    Another thing to consider is your radio buttons, radio buttons work as group, now logically someone filling the form cannot be checking both male and female, right? So you should group them so that only one is filled, you do this again by changing the name attribute:

    <!-- Again the `[]` is added because you have it repeating.-->
    <input type="radio" name="gender[]" value="6"/> Male <br>
    <input type="radio" name="gender[]" value="7"/> Female <br><br>
    

    Lastly, since age is an array, you need to loop through the values at the PHP side:

    function Calculate($age){
        foreach($age as $ages)
        {
          echo $ages;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题