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 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路