HTML to PHP arrays -
good evening everyone,
i've been searching , trying while build small program, want user input unknown amount of numbers (he can enter as like), , want make array out of them, everywhere on internet keeps using many "input type="text"...." boxes don't know how many user input.and don't want page keep refreshing if possible i'd send numbers @ once same page or page, create array out of them can complete program using functions based on array.
edit: first used <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 after read files , used piece of code , worked. thank member answered using jquery yet learn java couldn't use now. started last year software engineering student , still don't know , understand embarrassed when read codes others , don't understand
` $h = fopen("numtext.txt", "r");
if($h) { while (($data = fgetcsv($h, 1000, " ")) !== false) { foreach($data $num) $numbers[] = $num; } fclose($h); } print_r($numbers); echo count($numbers);`
here simple approach using 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 , change focus 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 backend "script.php" "stringify" array **/ this.sendtoserver = function(numberarray){ console.log("sending numbers server: ", numberarray); var stringforserver = json.stringify(numberarray); $.ajax({ url: "script.php", type: "post", data: ({numberarraystring : stringforserver}), success: function(msg){ alert("send server" + msg); } }); /** on server side in php (above called "script.php") do: $numberarray= json_decode($_post['numberarraystring']); // create php array stringify-json string **/ } /** on click: collect numbers input fields , send them 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 input field. press enter add additional numbers. </p> <div class="inputs"> <button class="sendtoserver">send server</button><br> </div>
Comments
Post a Comment