简体   繁体   English

表单输入和 JS

[英]Form Inputs and JS

need a little help with my coding.我的编码需要一点帮助。 I'm working on a project in which it will accept 5 different numbers and insert them into a PHP array.我正在开发一个项目,在该项目中它将接受 5 个不同的数字并将它们插入到 PHP 数组中。 The codes I have tried are written below.我试过的代码写在下面。 Both of them would make all the 5 contents of my array the same number, appreciate a little help please?他们两个都会使我的数组的所有 5 个内容相同,请感谢一点帮助? Never dealt with array in PHP yet.从未在 PHP 中处理过数组。 Sorry had to comment out the PHP since Stackoverflow is giving me error that it doesnt recognize the PHP code.抱歉不得不注释掉 PHP,因为 Stackoverflow 给我的错误是它无法识别 PHP 代码。 Thaaanks! Thaaanks!

 <form action="activity_1.php" method="post"> <input type="text" name="number"> <input type="submit" value="Submit" name="submit"> </form> <!-- So far these are the ones I've tried <?php $no = array(); for($i=1; $i<=5; $i++){ array_push($no, $_POST['number']); } ?> Also <?php $no = array(); for($i=1; $i<=5; $i++){ //$no[$i]= $_POST['number']; } ?> -->

enter code here

enter code here在此处输入代码

You may use one session variable to store the posted data.您可以使用一个会话变量来存储发布的数据。

So slightly amend your code into the following :因此,将您的代码稍微修改为以下内容:

<?php session_start(); ?>

<form action="#" method="post">

        <input type="text" name="number">
        <input type="submit" value="Submit" name="submit">
</form>



<?php
    if ( !isset($_SESSION["no"]) ) {
    $_SESSION["no"]=array();
    }

if (isset($_POST["number"])){
array_push($_SESSION["no"], $_POST['number']);
}



for($i=0; $i<5; $i++){
    if (isset($_SESSION["no"][$i])) {
       if ($_SESSION["no"][$i]!="") {
         echo " Number you entered was: " .$_SESSION["no"][$i] . "<br>";
        }
    }
}


?>

In my opinion the code can be further amended so that在我看来,代码可以进一步修改,以便

a) you should have a button to "clear" all the previous entered number; a) 你应该有一个按钮来“清除”之前输入的所有数字;

b) I don't know whether it is necessary, but you may wish to add a function to check whether a newly entered number is the same as one of the past numbers entered, and if they are the same, do not put it into the array. b) 不知道有没有必要,不过你不妨加一个功能,检查一个新输入的数字是否与过去输入的数字相同,如果相同,就不要放入数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM