简体   繁体   English

HTML 到 PHP 数组

[英]HTML to PHP arrays

good evening everyone,大家晚上好,

I've been searching and trying for a while to build a small program, I want the user to input an unknown amount of numbers (he can enter as much as he would like), and I want to make an array out of them, everywhere on the internet keeps using many "input type="text"...." boxes but I don't know how many the user will input.我一直在搜索并尝试构建一个小程序,我希望用户输入未知数量的数字(他可以输入任意数量的数字),我想用它们制作一个数组,互联网上的任何地方都在使用许多“输入类型=“文本”....”框,但我不知道用户会输入多少。 and I don't want the page to keep refreshing so if possible I'd like to send all the numbers at once to the same page or another page, create an array out of them so I can complete the program using functions based on that array.我不希望页面不断刷新,所以如果可能的话,我想将所有数字一次发送到同一页面或另一个页面,用它们创建一个数组,以便我可以使用基于该的函数完成程序大批。

EDIT: i first used a <form action="process.php" method=POST> insert values here: <input type="number" name=num><br> <input type="submit" value="send"> </form>编辑:我首先使用<form action="process.php" method=POST> insert values here: <input type="number" name=num><br> <input type="submit" value="send"> </form>

and didn't know what to do after that then I read about files and used this piece of code, and it worked.并且不知道在那之后该怎么做然后我阅读了文件并使用了这段代码,并且它起作用了。 thank you to the member who answered using jquery but I am yet to learn java so I couldn't really use it now.感谢使用 jquery 回答的成员,但我还没有学习 Java,所以我现在无法真正使用它。 I started last year as a software engineering student and still don't really know and understand much so I am embarrassed when I read codes by others and don't understand去年开始学软件工程的,到现在还不是很懂懂很多,看别人写的代码看不懂很尴尬

` $h = fopen("numtext.txt", "r"); ` $h = fopen("numtext.txt", "r");

if($h) {
    while (($data = fgetcsv($h, 1000, " ")) !== FALSE) {
        foreach($data as $num)
            $numbers[] = $num;
    }
    fclose($h);
}

print_r($numbers);
echo count($numbers);`

Here a simple approach using jQuery这是一个使用jQuery的简单方法

 var InputHandler = function( $el ){ var t = this; this.$el = $el; this.sendToServerButton = $el.find("button.sendToServer"); var newInputElement = '<input type="text" /><br>'; this.$el.append(newInputElement); /** on enter: add newInputElement and change focus to the new **/ this.$el.on("keyup", "input", function(e){ if (e.which !== 13) { return false; } t.$el.append(newInputElement); t.$el.find("input:last").focus(); }); /** send to the backend "script.php" as a "stringify" array **/ this.sendToServer = function(numberArray){ console.log("sending numbers to the server: ", numberArray); var stringForServer = JSON.stringify(numberArray); $.ajax({ url: "script.php", type: "POST", data: ({numberArrayString : stringForServer}), success: function(msg){ alert("send to server" + msg); } }); /** on the server side in PHP (above called "script.php") do: $numberArray= json_decode($_POST['numberArrayString']); // create PHP array from stringify-json string **/ } /** on click: collect numbers from the input fields and send them to the server **/ this.sendToServerButton.on("click", function(){ var numbers = []; $el.find("input").each(function(){ var numberAsString = $(this).val(); var number = parseInt(numberAsString); numbers.push( number ); }); t.sendToServer(numbers); }); } var inputHandler = new InputHandler( $(".inputs") );
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> Enter numbers in the input field. Press enter to add additional numbers. </p> <div class="inputs"> <button class="sendToServer">send to server</button><br> </div>

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

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